Example #1
0
 /**
  * dispatch
  * @throws Exception
  */
 public function dispatch()
 {
     if ($this->routeType == 1) {
         //require_once $this->config['route']['includeRulesFile'];
         Route::haltOnMatch();
         Route::error(function ($route) {
             header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
             throw new Exception('Page Not Found', "This routeļ¼š\"{$_SERVER['REQUEST_METHOD']}: {$route}\" No matching items!");
         });
         Route::dispatch();
         $this->end();
     }
     $this->parseRequest();
     // Joining together the controller class name
     if ($this->module == $this->defaultModule) {
         $className = $this->controllerNamespace . '\\' . ucfirst($this->controller) . 'Controller';
     } else {
         // Joining together the controller class name
         if (!empty($this->config['modules'][$this->module]['controllerNamespace'])) {
             $className = $this->config['modules'][$this->module]['controllerNamespace'] . '\\' . ucfirst($this->controller) . 'Controller';
         } else {
             $className = 'app\\modules\\' . $this->module . '\\controllers\\' . ucfirst($this->controller) . 'Controller';
         }
     }
     // check controller class exists
     if (!class_exists($className)) {
         header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
         throw new Exception('Page Not Found', 'Controller ' . $className . ' not exists!');
     }
     // Instantiate the controller
     $controllerObj = new $className();
     if (!(!is_null($controllerObj) && $controllerObj instanceof Controller)) {
         header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
         throw new Exception('Page Not Found', 'Controller ' . $className . ' has error!');
     }
     $action = 'action' . ucfirst($this->action);
     // check action method exists
     if (!method_exists($controllerObj, $action)) {
         header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
         throw new Exception('Page Not Found', 'Action ' . get_class($controllerObj) . '::' . $action . ' not exists!');
     }
     $this->currentController = $controllerObj;
     // action
     $controllerObj->{$action}();
 }