Yii源码阅读笔记(三十二)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Yii源码阅读笔记(三十二)相关的知识,希望对你有一定的参考价值。

web/Application类的注释,继承base/Application类,针对web应用的一些处理:

  1 namespace yii\web;
  2 
  3 use Yii;
  4 use yii\base\InvalidRouteException;
  5 
  6 /**
  7  * Application is the base class for all web application classes.
  8  * Application 是所有web应用的基类
  9  *
 10  * @property string $homeUrl The homepage URL.
 11  * @property Session $session The session component. This property is read-only.
 12  * @property User $user The user component. This property is read-only.
 13  *
 14  * @author Qiang Xue <[email protected]>
 15  * @since 2.0
 16  */
 17 class Application extends \yii\base\Application
 18 {
 19     /**
 20      * @var string the default route of this application. Defaults to ‘site‘.
 21      * @var string 默认的路由,默认值为‘site’.
 22      */
 23     public $defaultRoute = ‘site‘;
 24     /**
 25      * @var array the configuration specifying a controller action which should handle
 26      * all user requests. This is mainly used when the application is in maintenance mode
 27      * and needs to handle all incoming requests via a single action.
 28      * @var array 配置项,指定一个控制器处理所有用户的请求,主要用于维护模式
 29      * The configuration is an array whose first element specifies the route of the action.
 30      * The rest of the array elements (key-value pairs) specify the parameters to be bound
 31      * to the action. For example,
 32      * 配置项是一个第一个元素指定Action的路由,其余元素为参数的数组,例如:
 33      *
 34      * ```php
 35      * [
 36      *     ‘offline/notice‘,
 37      *     ‘param1‘ => ‘value1‘,
 38      *     ‘param2‘ => ‘value2‘,
 39      * ]
 40      * ```
 41      *
 42      * Defaults to null, meaning catch-all is not used.
 43      */
 44     public $catchAll;
 45     /**
 46      * @var Controller the currently active controller instance
 47      * @var Controller 默认的控制器实例
 48      */
 49     public $controller;
 50 
 51 
 52     /**
 53      * @inheritdoc
 54      */
 55     protected function bootstrap()
 56     {
 57         $request = $this->getRequest();//获取请求的组件
 58         Yii::setAlias(‘@webroot‘, dirname($request->getScriptFile()));//设置webroot别名,该别名指向正在运行的应用的入口文件 index.php 所在的目录
 59         Yii::setAlias(‘@web‘, $request->getBaseUrl());//定义别名web,指向当前应用的根URL,主要用于前端
 60 
 61         parent::bootstrap();//调用父类方法,初始化应用组件
 62     }
 63 
 64     /**
 65      * Handles the specified request.
 66      * 处理指定的请求
 67      * @param Request $request the request to be handled
 68      * @return Response the resulting response
 69      * @throws NotFoundHttpException if the requested route is invalid
 70      */
 71     public function handleRequest($request)
 72     {
 73         if (empty($this->catchAll)) {
 74             list ($route, $params) = $request->resolve();//取出路由及参数
 75         } else {
 76             $route = $this->catchAll[0];
 77             $params = $this->catchAll;
 78             unset($params[0]);
 79         }
 80         try {
 81             Yii::trace("Route requested: ‘$route‘", __METHOD__);
 82             $this->requestedRoute = $route;
 83             $result = $this->runAction($route, $params);//运行控制器中的Acition
 84             if ($result instanceof Response) {
 85                 return $result;
 86             } else {
 87                 /**
 88                  *这个是加载yii\base\Response类,在外部可以Yii::$app->get(‘response‘)、Yii::$app->getResponse()、Yii::$app->response 等等方式来加载response类
 89                  *主要用来加载http状态,及头信息,如301,302,404,ajax头等等的获取
 90                  */
 91                 $response = $this->getResponse();
 92                 if ($result !== null) {
 93                     $response->data = $result;
 94                 }
 95 
 96                 return $response;
 97             }
 98         } catch (InvalidRouteException $e) {
 99             throw new NotFoundHttpException(Yii::t(‘yii‘, ‘Page not found.‘), $e->getCode(), $e);
100         }
101     }
102 
103     private $_homeUrl;
104 
105     /**
106      * @return string the homepage URL
107      * @return string 返回主页的URL
108      */
109     public function getHomeUrl()
110     {
111         if ($this->_homeUrl === null) {
112             if ($this->getUrlManager()->showScriptName) {//如果请求路径中显示入口脚本
113                 return $this->getRequest()->getScriptUrl();//返回入口脚本的相对路径
114             } else {
115                 return $this->getRequest()->getBaseUrl() . ‘/‘;//否则返回BaseUrl,即去掉入口脚本名和结束斜线的路径
116             }
117         } else {
118             return $this->_homeUrl;
119         }
120     }
121 
122     /**
123      * @param string $value the homepage URL
124      * 设置主页URL
125      */
126     public function setHomeUrl($value)
127     {
128         $this->_homeUrl = $value;
129     }
130 
131     /**
132      * Returns the session component.
133      * 返回session组件对象
134      * @return Session the session component.
135      */
136     public function getSession()
137     {
138         return $this->get(‘session‘);
139     }
140 
141     /**
142      * Returns the user component.
143      * 返回user组件对象
144      * @return User the user component.
145      */
146     public function getUser()
147     {
148         return $this->get(‘user‘);
149     }
150 
151     /**
152      * @inheritdoc
153      * 定义核心组件,用于程序初始化时加载
154      */
155     public function coreComponents()
156     {
157         return array_merge(parent::coreComponents(), [
158             ‘request‘ => [‘class‘ => ‘yii\web\Request‘],
159             ‘response‘ => [‘class‘ => ‘yii\web\Response‘],
160             ‘session‘ => [‘class‘ => ‘yii\web\Session‘],
161             ‘user‘ => [‘class‘ => ‘yii\web\User‘],
162             ‘errorHandler‘ => [‘class‘ => ‘yii\web\ErrorHandler‘],
163         ]);
164     }
165 }

 

以上是关于Yii源码阅读笔记(三十二)的主要内容,如果未能解决你的问题,请参考以下文章

Yii源码阅读笔记(三十)

Yii源码阅读笔记(三十四)

Yii源码阅读笔记(三十三)

Yii源码阅读笔记(三十一)

2022/01/30thinkphp源码无差别阅读(三十二)

yii2源码学习笔记(十二)