Kaynağa Gözat

fixed errors

朱金辉 2 yıl önce
ebeveyn
işleme
b18d27fdc8

+ 19 - 4
src/Application.php

@@ -195,6 +195,7 @@ class Application
      * @param string $ini 配置文件路径
      * @param string $env 环境
      * @return $this
+     * @throws ClassNotFound
      */
     public function setAppConfigure($ini, $env = '')
     {
@@ -203,9 +204,8 @@ class Application
         $this->setAppIniFile($ini);
         if (!Register::setAppConfigure($ini, $env)) throw new FileNotFound($ini, 404);
         //载入request方法
-        $this->request = Psr4::getInstance()->loadClass('\Qii\Request\Http');
-        $this->helper->load(self::$workspace);
-        $this->dispatcher = $this->setDispatcher();
+        $this->request = $this->getRequest();
+        $this->dispatcher = $this->getDispatcher();
         if (!$this->dispatcher instanceof Dispatcher) {
             throw new \Exception('Dispatcher must instance of Qii\Base\Dispatcher', __LINE__);
         }
@@ -213,6 +213,8 @@ class Application
         Setting::getInstance()->setDefaultControllerAction();
         Setting::getInstance()->setDefaultNamespace();
         Setting::getInstance()->setDefaultLanguage();
+
+        $this->helper->load(self::$workspace);
         return $this;
     }
 
@@ -306,12 +308,25 @@ class Application
         $this->middleware = array_merge($this->middleware, $middleware);
         return $this;
     }
+
+    /**
+     * 实例化request并返回
+     *
+     * @return mixed
+     * @throws ClassNotFound
+     */
+    public function getRequest() {
+        if ($this->request !== null) {
+            return $this->request;
+        }
+        return Psr4::getInstance()->loadClass('\Qii\Request\Http');
+    }
     /**
      * Set Dispatcher
      * @return void
      * @throws ClassNotFound
      */
-    public function setDispatcher() {
+    public function getDispatcher() {
         if ($this->dispatcher !== null) {
             return $this->dispatcher;
         }

+ 2 - 1
src/Base/Controller.php

@@ -7,6 +7,7 @@ namespace Qii\Base;
 
 use Qii\Autoloader\Factory;
 use \Qii\Autoloader\Psr4;
+use Qii\Driver\Model;
 use \Qii\View\Intf as viewIntf;
 
 /**
@@ -129,7 +130,7 @@ abstract class Controller
      */
     final public function enableDB()
     {
-        return $this->db = new \Qii\Driver\Model();
+        return $this->db = new Model();
     }
     
     /**

+ 7 - 7
src/Base/Dispatcher.php

@@ -124,7 +124,7 @@ class Dispatcher
         $this->controllerCls->controller = $controllerCls;
         $this->controllerCls->controllerId = $controller;
         $this->controllerCls->actionId = $action;
-
+        $realAction = $action . Register::get(Consts::APP_DEFAULT_ACTION_SUFFIX);
         //查看是否设置了当前action的对应关系,如果设置了就走对应关系里边的,否则走当前类中的
         if ($this->controllerCls->actions
             && isset($this->controllerCls->actions[$action])
@@ -138,8 +138,8 @@ class Dispatcher
             $this->actionCls->actionId = $action;
             $this->actionCls->controllerId = $this->controllerCls->controllerId;
             //支持多个action对应到同一个文件,如果对应的文件中存在指定的方法就直接调用
-            if (method_exists($this->actionCls, $action . Register::get(Consts::APP_DEFAULT_ACTION_SUFFIX))) {
-                $this->actionCls->response = call_user_func_array(array($this->actionCls, $action. Register::get(Consts::APP_DEFAULT_ACTION_SUFFIX)), $funcArgs);
+            if (method_exists($this->actionCls, $realAction)) {
+                $this->actionCls->response = call_user_func_array(array($this->actionCls, $realAction), $funcArgs);
             }
             if(method_exists($this->actionCls, 'initialization')) {
                 call_user_func_array(array($this->actionCls, 'initialization'), array($this->actionCls));
@@ -153,10 +153,10 @@ class Dispatcher
             call_user_func_array(array($this->controllerCls, 'initialization'), array($this->controllerCls));
         }
         array_shift($funcArgs);
-        $actionName = $action . Register::get(Consts::APP_DEFAULT_ACTION_SUFFIX);
-        if (!method_exists($this->controllerCls, $actionName) && !method_exists($this->controllerCls, '__call')) {
-            throw new MethodNotFound(\Qii::i(1101, $controller . '->' . $actionName), __LINE__);
+
+        if (!method_exists($this->controllerCls, $realAction) && !method_exists($this->controllerCls, '__call')) {
+            throw new MethodNotFound(\Qii::i(1101, $controller . '->' . $realAction), __LINE__);
         }
-        return call_user_func_array(array($this->controllerCls, $actionName), $funcArgs);
+        return call_user_func_array(array($this->controllerCls, $realAction), $funcArgs);
     }
 }

+ 4 - 1
src/Base/Request.php

@@ -4,6 +4,7 @@ namespace Qii\Base;
 
 use Qii\Config\Consts;
 use Qii\Config\Register;
+use Qii\Request\Url;
 
 abstract class Request
 {
@@ -46,7 +47,9 @@ abstract class Request
             $this->setParam($key, $val);
         }
         $rewriteRule = \Qii::getInstance()->appConfigure(Consts::APP_SITE_METHOD);
-        $this->url = new \Qii\Request\Url($rewriteRule);
+        //如果获取配置文件失败,就是会用默认短链的形式
+        if ($rewriteRule == null) $rewriteRule = "Short";
+        $this->url = new Url($rewriteRule);
         $this->host = IS_CLI ? '' : $_SERVER['HTTP_HOST'];
         $params = (array)$this->url->getParams();
         if(count($params) > 0) $this->params = array_merge($this->params, $params);

+ 1 - 0
src/Exceptions/Error.php

@@ -116,6 +116,7 @@ class Error
             if(substr($controller, 0, 1) != '\\') {
                 $controllerCls = Register::get(\Qii\Config\Consts::APP_DEFAULT_CONTROLLER_PREFIX) . '\\' . $controller;
             }
+            $action = preg_replace('/(Action)$/i', "", $action);
             $filePath = Psr4::getInstance()->searchMappedFile($controllerCls);
             if (!is_file($filePath)) {
                 if ($env == 'product') return '';

+ 5 - 2
src/Exceptions/Errors.php

@@ -13,7 +13,6 @@ if (class_exists('\Qii\Exceptions\Errors', false)) {
 class Errors extends \Exception
 {
     const VERSION = '1.2';
-    
     /**
      * 获取两个文件的相对路径
      * @param String $cur
@@ -51,6 +50,9 @@ class Errors extends \Exception
     public static function getError($e)
     {
         $message = array();
+        if (\Qii::getInstance()->request == null) {
+            \Qii::getInstance()->request = \Qii::getInstance()->getRequest();
+        }
         if (\Qii::getInstance()->request->isXmlHttpRequest()) {
             $code = $e->getCode();
             if ($code == 0) $code = 1;
@@ -81,6 +83,7 @@ class Errors extends \Exception
             if(substr($controller, 0, 1) != '\\') {
                 $controllerCls = Register::get(Consts::APP_DEFAULT_CONTROLLER_PREFIX) . '\\' . $controller;
             }
+            $action = preg_replace('/(Action)$/i', "", $action);
             $filePath = Psr4::getInstance()->searchMappedFile($controllerCls);
             if (!is_file($filePath)) {
                 if ($env == 'product') return '';
@@ -92,7 +95,7 @@ class Errors extends \Exception
             \Qii::getInstance()->request->setActionName($action);
             //dispatcher是run 的时候初始化,所以这里可能会是空
             if (\Qii::getInstance()->dispatcher == null) {
-                return \Qii::getInstance()->setDispatcher()->setRequest(\Qii::getInstance()->request)->dispatch($controller, $action, $e);
+                return \Qii::getInstance()->getDispatcher()->setRequest(\Qii::getInstance()->request)->dispatch($controller, $action, $e);
             }
             return \Qii::getInstance()->dispatcher->setRequest(\Qii::getInstance()->request)->dispatch($controller, $action, $e);
         }