/** * Construct the object * * @param string $uri */ public function __construct($uri, array $config = null) { parent::__construct($uri, $config); // first, check the URI for duplicate slashes - they are not allowed // if you must pass duplicate slashes in URL, then urlencode them if (strpos($uri, '//') !== false) { header('Location: ' . str_replace('//', '/', $uri)); exit(0); } $slash = DS; // There are two possible scenarios: // 1. The first part of URL leads to the module controller // 2. The first part of URL leads to the default controller if ($this->uri[1] == '') { $this->controllerUrl = 'index'; $this->controllerClass = 'IndexController'; } else { $this->controllerUrl = strtolower($this->uri[1]); $this->controllerClass = str_replace(' ', '', ucwords(str_replace(array('-', '.'), ' ', $this->controllerUrl))) . 'Controller'; } // Now we have the controller class name detected, but, should it be // taken from module or from default controllers? $moduleDir = Application::getApplicationPath("modules{$slash}{$this->controllerUrl}"); if (is_dir($moduleDir)) { // ok, it is a module with module/controller/action path $moduleUrl = $this->controllerUrl; $this->moduleUrl = $moduleUrl; if (isset($this->uri[2]) && $this->uri[2] != '') { $this->controllerUrl = strtolower($this->uri[2]); $this->controllerClass = str_replace(' ', '', ucwords(str_replace(array('-', '.'), ' ', $this->controllerUrl))) . 'Controller'; } else { $this->controllerUrl = 'index'; $this->controllerClass = 'IndexController'; } $this->controllerPath = "{$moduleDir}{$slash}controllers{$slash}{$this->controllerClass}.php"; $mainControllerExists = true; if (!is_file($this->controllerPath)) { // lets try with default controller when requested one is not here $this->controllerPath = Application::getApplicationPath("modules{$slash}{$moduleUrl}{$slash}controllers{$slash}IndexController.php"); if (!is_file($this->controllerPath)) { // Even IndexController is missing. Can not resolve that. Log::notice("Can not find {$this->controllerClass} nor IndexController in {$moduleDir}{$slash}controllers"); Application::error(404, 'Page not found'); } $mainControllerExists = false; $this->controllerClass = 'IndexController'; } if ($mainControllerExists) { if (!isset($this->uri[3]) || $this->uri[3] == '') { $this->actionUrl = 'index'; $this->actionMethod = 'index'; } else { $this->actionUrl = strtolower($this->uri[3]); $this->actionMethod = ucwords(str_replace(array('-', '.'), ' ', $this->actionUrl)); $this->actionMethod = str_replace(' ', '', $this->actionMethod); $this->actionMethod = strtolower(substr($this->actionMethod, 0, 1)) . substr($this->actionMethod, 1); } } else { if (isset($this->uri[2]) && $this->uri[2] != '') { $this->actionUrl = strtolower($this->uri[2]); $this->actionMethod = ucwords(str_replace(array('-', '.'), ' ', $this->actionUrl)); $this->actionMethod = str_replace(' ', '', $this->actionMethod); $this->actionMethod = strtolower(substr($this->actionMethod, 0, 1)) . substr($this->actionMethod, 1); } else { $this->actionUrl = 'index'; $this->actionMethod = 'index'; } } // and now, configure the include paths according to the case $basePath = Application::getApplicationPath(); $includePath = array("{$moduleDir}{$slash}controllers", "{$moduleDir}{$slash}modles", "{$moduleDir}{$slash}library", get_include_path()); set_include_path(implode(PATH_SEPARATOR, $includePath)); } else { // ok, it is the default controller/action $this->controllerPath = Application::getApplicationPath("controllers{$slash}{$this->controllerClass}.php"); $mainControllerExists = true; if (!is_file($this->controllerPath)) { $this->controllerPath = Application::getApplicationPath("controllers{$slash}IndexController.php"); if (!is_file($this->controllerPath)) { // Even IndexController is missing. Can not resolve that. Log::notice("Can not find {$this->controllerClass} nor IndexController in " . Application::getApplicationPath('controllers')); Application::error(404, 'Page not found'); } $mainControllerExists = false; $this->controllerClass = 'IndexController'; } if ($mainControllerExists) { if (!isset($this->uri[2]) || $this->uri[2] == '') { $this->actionUrl = 'index'; $this->actionMethod = 'index'; } else { $this->actionUrl = strtolower($this->uri[2]); $this->actionMethod = ucwords(str_replace(array('-', '.'), ' ', $this->actionUrl)); $this->actionMethod = str_replace(' ', '', $this->actionMethod); $this->actionMethod = strtolower(substr($this->actionMethod, 0, 1)) . substr($this->actionMethod, 1); } } else { $this->actionUrl = strtolower($this->uri[1]); $this->actionMethod = ucwords(str_replace(array('-', '.'), ' ', $this->actionUrl)); $this->actionMethod = str_replace(' ', '', $this->actionMethod); $this->actionMethod = strtolower(substr($this->actionMethod, 0, 1)) . substr($this->actionMethod, 1); } // and now, configure the include paths according to the case $basePath = Application::getApplicationPath(); Application::addIncludePath(array($basePath . 'controllers', $basePath . 'models', $basePath . 'library')); } $this->isAjax = isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' || isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false; $controllerClassName = $this->getControllerClass(); $controllerInstance = new $controllerClassName(); $alwaysRestful = isset($this->config['always_restful']) && $this->config['always_restful'] === true; if ($alwaysRestful || property_exists($controllerInstance, 'restful') && $controllerInstance::$restful === true) { // it is restful $this->actionMethod = strtolower($_SERVER['REQUEST_METHOD']) . ucfirst($this->actionMethod); } else { if ($this->isAjax) { $this->actionMethod .= 'Ajax'; } else { $this->actionMethod .= 'Action'; } } $this->controllerInstance = $controllerInstance; }
/** * Construct the object * @param string $uri */ public function __construct($uri, array $config = null) { if ($config === null || count($config) == 0 || !isset($config['module_param']) || !isset($config['controller_param']) || !isset($config['action_param'])) { throw new Exception('Route config options are missing'); } parent::__construct($uri, $config); if (isset($_GET[$config['controller_param']])) { $c = trim($_GET[$config['controller_param']]); if ($c == '') { $this->controllerUrl = 'index'; $this->controllerClass = 'IndexController'; } else { $this->controllerUrl = strtolower($c); $this->controllerClass = str_replace(' ', '', ucwords(str_replace(array('-', '.'), ' ', $this->controllerUrl))) . 'Controller'; } } else { $this->controllerUrl = 'index'; $this->controllerClass = 'IndexController'; } // Now we have the controller class name detected, but, should it be // taken from module or from default controllers? // TODO: Zavrsiti ovo! $moduleDir = Application::getApplicationPath() . 'modules' . DS . $this->controllerUrl; if (is_dir($moduleDir)) { // ok, it is a module with module/controller/action path $moduleUrl = $this->controllerUrl; $this->moduleUrl = $moduleUrl; $a = isset($_GET[$config['action_param']]) ? trim($_GET[$config['action_param']]) : ''; if ($a != '') { $this->controllerUrl = strtolower($a); $this->controllerClass = str_replace(' ', '', ucwords(str_replace(array('-', '.'), ' ', $this->controllerUrl))) . 'Controller'; } else { $this->controllerUrl = 'index'; $this->controllerClass = 'IndexController'; } $this->controllerPath = $moduleDir . DS . 'controllers' . DS . $this->controllerClass . '.php'; $mainControllerExists = true; if (!is_file($this->controllerPath)) { $this->controllerPath = Application::getApplicationPath() . 'modules' . DS . $moduleUrl . DS . 'controllers' . DS . 'IndexController.php'; if (!is_file($this->controllerPath)) { // Even IndexController is missing. Can not resolve that. if (Application::inDevelopment()) { $controllersPath = $moduleDir . DS . 'controllers'; \Koldy\Log::debug("Can not find {$this->controllerClass} nor IndexController in {$controllersPath}"); } Application::error(404, 'Page not found'); } $mainControllerExists = false; $this->controllerClass = 'IndexController'; } if ($mainControllerExists) { if (!isset($this->uri[3]) || $this->uri[3] == '') { $this->actionUrl = 'index'; $this->actionMethod = 'index'; } else { $this->actionUrl = strtolower($this->uri[3]); $this->actionMethod = ucwords(str_replace(array('-', '.'), ' ', $this->actionUrl)); $this->actionMethod = str_replace(' ', '', $this->actionMethod); $this->actionMethod = strtolower(substr($this->actionMethod, 0, 1)) . substr($this->actionMethod, 1); } } else { if (isset($this->uri[2]) && $this->uri[2] != '') { $this->actionUrl = strtolower($this->uri[2]); $this->actionMethod = ucwords(str_replace(array('-', '.'), ' ', $this->actionUrl)); $this->actionMethod = str_replace(' ', '', $this->actionMethod); $this->actionMethod = strtolower(substr($this->actionMethod, 0, 1)) . substr($this->actionMethod, 1); } else { $this->actionUrl = 'index'; $this->actionMethod = 'index'; } } // and now, configure the include paths according to the case Application::addIncludePath(array($moduleDir . DS . 'controllers' . DS, $moduleDir . DS . 'models' . DS, $moduleDir . DS . 'library' . DS, Application::getApplicationPath() . 'controllers' . DS, Application::getApplicationPath() . 'models' . DS, Application::getApplicationPath() . 'library' . DS)); } else { // ok, it is the default controller/action $this->controllerPath = Application::getApplicationPath() . 'controllers' . DS . $this->controllerClass . '.php'; $mainControllerExists = true; if (!is_file($this->controllerPath)) { $this->controllerPath = Application::getApplicationPath() . 'controllers' . DS . 'IndexController.php'; if (!is_file($this->controllerPath)) { // Even IndexController is missing. Can not resolve that. if (Application::inDevelopment()) { $controllersPath = Application::getApplicationPath() . 'controllers'; \Koldy\Log::debug("Can not find {$this->controllerClass} nor IndexController in {$controllersPath}"); } Application::error(404, 'Page not found'); } $mainControllerExists = false; $this->controllerClass = 'IndexController'; } if ($mainControllerExists) { if (!isset($this->uri[2]) || $this->uri[2] == '') { $this->actionUrl = 'index'; $this->actionMethod = 'index'; } else { $this->actionUrl = strtolower($this->uri[2]); $this->actionMethod = ucwords(str_replace(array('-', '.'), ' ', $this->actionUrl)); $this->actionMethod = str_replace(' ', '', $this->actionMethod); $this->actionMethod = strtolower(substr($this->actionMethod, 0, 1)) . substr($this->actionMethod, 1); } } else { $this->actionUrl = strtolower($this->uri[1]); $this->actionMethod = ucwords(str_replace(array('-', '.'), ' ', $this->actionUrl)); $this->actionMethod = str_replace(' ', '', $this->actionMethod); $this->actionMethod = strtolower(substr($this->actionMethod, 0, 1)) . substr($this->actionMethod, 1); } // and now, configure the include paths according to the case Application::addIncludePath(array(Application::getApplicationPath() . 'controllers' . DS, Application::getApplicationPath() . 'models' . DS, Application::getApplicationPath() . 'library' . DS)); } $this->isAjax = isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' || isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false; if ($this->isAjax) { $this->actionMethod .= 'Ajax'; } else { $this->actionMethod .= 'Action'; } }