Пример #1
0
 /**
  * Map URL to controller and action
  *
  * @return void
  */
 public function dispatch()
 {
     if (Config::getSoul()->URL_MODE == 'QUERY_STRING') {
         $this->_uri = explode('?', $this->_uri, 2);
         $_SERVER['QUERY_STRING'] = isset($this->_uri[1]) ? $this->_uri[1] : '';
         $this->_uri = $this->_uri[0];
         parse_str($_SERVER['QUERY_STRING'], $_GET);
     }
     define('URI', $this->_uri);
     switch ($this->_uri) {
         case 'favicon.ico':
             Response::getSoul()->setHeader('Content-Type', 'image/x-icon');
             Response::getSoul()->setCacheHeader();
             echo base64_decode(preg_replace('#^data:image/\\w+;base64,#i', '', Helper::logo()));
             exit;
         case 'kotori-php-system-route/highlight-github.css':
             Response::getSoul()->setHeader('Content-Type', 'text/css; charset=utf-8');
             Response::getSoul()->setCacheHeader();
             echo file_get_contents(Helper::getComposerVendorPath() . '/components/highlightjs/styles/github.css');
             exit;
         case 'kotori-php-system-route/highlight.js':
             Response::getSoul()->setHeader('Content-Type', 'text/javascript; charset=utf-8');
             Response::getSoul()->setCacheHeader();
             echo file_get_contents(Helper::getComposerVendorPath() . '/components/highlightjs/highlight.pack.min.js');
             exit;
     }
     $parsedRoute = $this->parseRoutes($this->_uri);
     if ($parsedRoute) {
         $this->_uri = $parsedRoute;
     } else {
         throw new \Exception('Request URI ' . $this->_uri . ' is not Matched by any route.');
     }
     $this->_uris = $this->_uri != '' ? explode('/', trim($this->_uri, '/')) : array();
     //Clean uris
     foreach ($this->_uris as $key => $value) {
         if ($value == '') {
             unset($this->_uris[$key]);
         }
     }
     $this->_uris = array_merge($this->_uris);
     $this->_controller = $this->getController();
     $this->_action = $this->getAction();
     //Define some variables
     define('CONTROLLER_NAME', $this->_controller);
     define('ACTION_NAME', $this->_action);
     define('PUBLIC_DIR', Request::getSoul()->getBaseUrl() . 'public');
     //If is already initialized
     $prefix = Config::getSoul()->NAMESPACE_PREFIX;
     $controllerClassName = $prefix . 'controllers\\' . $this->_controller;
     if (isset($this->_controllers[$this->_controller])) {
         $class = $this->_controllers[$this->_controller];
     } else {
         $class = new $controllerClassName();
         $this->_controllers[$this->_controller] = $class;
     }
     if (!class_exists($controllerClassName)) {
         throw new \Exception('Request Controller ' . $this->_controller . ' is not Found.');
     }
     if (!method_exists($class, $this->_action)) {
         throw new \Exception('Request Action ' . $this->_action . ' is not Found.');
     }
     //Parse params from uri
     $this->_params = $this->getParams();
     //Do some final cleaning of the params
     $_GET = array_merge($this->_params, $_GET);
     $_REQUEST = array_merge($_POST, $_GET, $_COOKIE);
     Response::getSoul()->setHeader('X-Powered-By', 'Kotori');
     Response::getSoul()->setHeader('Cache-control', 'private');
     //Call the requested method
     call_user_func_array(array($class, $this->_action), $this->_params);
 }