Ejemplo n.º 1
0
 /**
  * 执行指定的 Action 方法
  *
  * @param QContext $context
  * @param array $args
  *
  * @return mixed
  */
 protected function _executeAction(QContext $context, array $args = array())
 {
     // 检查是否有权限访问
     $controller_name = $context->controller_name;
     $action_name = $context->action_name;
     $namespace = $context->namespace;
     $module = $context->module_name;
     QLog::log(sprintf('Execute controller action: "%s".', $context->getRequestUDI()));
     if (!$this->checkAuthorized($controller_name, $action_name, $namespace, $module)) {
         $response = call_user_func($context->getIni('dispatcher_on_access_denied'), $context);
     } else {
         // 尝试载入控制器
         $class_name = $context->getIni('controller_class_prefix') . 'Controller_';
         if ($namespace) {
             $class_name .= ucfirst($namespace) . '_';
         }
         $class_name .= ucfirst(str_replace('_', '', $controller_name));
         $app_config = self::getAppConfig($this->_appid);
         if ($module) {
             $dir = $app_config['ROOT_DIR'] . "/modules/{$module}/controller";
         } else {
             $dir = $app_config['ROOT_DIR'] . "/app/controller";
         }
         if ($namespace) {
             $dir .= DS . $namespace;
         }
         // 构造控制器对象
         try {
             $filename = $controller_name . '_controller.php';
             Q::loadClassFile($filename, array($dir), $class_name);
         } catch (QException $ex) {
             $response = call_user_func($this->context->getIni('dispatcher_on_action_not_found'), $context);
             if (is_null($response)) {
                 $response = '';
             }
         }
         if (!isset($response)) {
             $controller = new $class_name($this, $context);
             /* @var $controller QController_Abstract */
             if ($context->isAJAX()) {
                 $controller->view = null;
             }
             $response = $controller->_execute($args);
         }
     }
     if (is_object($response) && method_exists($response, 'execute')) {
         $response = $response->execute();
     } elseif ($response instanceof QController_Forward) {
         // 更新 flash message
         $key = $this->context->getIni('app_flash_message_key');
         unset($_SESSION[$key]);
         $response = $this->_executeAction($response->context, $response->args);
     }
     return $response;
 }