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 }
时间: 2024-10-29 10:46:15

Yii源码阅读笔记(三十二)的相关文章

Yii源码阅读笔记(十二)

Action类,控制器中方法的基类: 1 namespace yii\base; 2 3 use Yii; 4 5 /** 6 * Action is the base class for all controller action classes. 7 * Action是所有控制器方法的基类 8 * Action provides a way to reuse action method code. An action method in an Action 9 * class can be

Yii源码阅读笔记(十八)

View中的查找视图文件方法和渲染文件方法 1 /** 2 * Finds the view file based on the given view name. 3 * 通过view文件名查找view文件 4 * @param string $view the view name or the path alias of the view file. Please refer to [[render()]] 5 * on how to specify this parameter. 6 * @

Yii源码阅读笔记(十五)

Model类,集中整个应用的数据和业务逻辑——验证 /** * Returns the attribute labels. * 返回属性的标签 * * Attribute labels are mainly used for display purpose. For example, given an attribute * `firstName`, we can declare a label `First Name` which is more user-friendly and can *

Yii源码阅读笔记(十四)

Model类,集中整个应用的数据和业务逻辑——场景.属性和标签: 1 /** 2 * Returns a list of scenarios and the corresponding active attributes. 3 * An active attribute is one that is subject to validation in the current scenario. 4 * 返回所有场景及与之对应的 active 属性的列表 5 * active 属性是指在默认场景中验

Yii源码阅读笔记(十六)

Model类,集中整个应用的数据和业务逻辑—— /** * Generates a user friendly attribute label based on the give attribute name. * 生成一个对用户友好的属性标签,将属性名中的下划线.破折号.点替换为空格,并且每个单词的首字母大写 * This is done by replacing underscores, dashes and dots with blanks and * changing the first

Yii源码阅读笔记(十)

控制器类,所有控制器的基类,用于调用模型和布局,输出到视图 1 namespace yii\base; 2 3 use Yii; 4 5 /** 6 * Controller is the base class for classes containing controller logic. 7 * 控制器,是所用控制器类的基类 8 * 9 * @property Module[] $modules 只读属性 当前控制器的所有模块 10 * 11 * @property string $rout

Yii源码阅读笔记(十九)

View中渲染view视图文件的前置和后置方法,以及渲染动态内容的方法: 1 /** 2 * @return string|boolean the view file currently being rendered. False if no view file is being rendered. 3 */ 4 public function getViewFile() 5 { 6 return end($this->_viewFiles);//返回[_viewFiles]中的最后一个view

Yii源码阅读笔记 - 日志组件

?使用 Yii框架为开发者提供两个静态方法进行日志记录: Yii::log($message, $level, $category);Yii::trace($message, $category); 两者的区别在于后者依赖于应用开启调试模式,即定义常量YII_DEBUG: defined('YII_DEBUG') or define('YII_DEBUG', true); Yii::log方法的调用需要指定message的level和category.category是格式为“xxx.yyy.z

Yii源码阅读笔记(一)

今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于web目录的index.php,用于启动web应用和配置一些路径参数. index.php—— 1 // comment out the following two lines when deployed to production 2 defined('YII_DEBUG') or define('Y

three.js 源码注释(三十二)Scenes/FogExp2.js

商域无疆 (http://blog.csdn.net/omni360/) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:商域无疆 -  本博客专注于 敏捷开发及移动和物联设备研究:数据可视化.GOLANG.Html5.WEBGL.THREE.JS,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 俺也是刚开始学,好多地儿肯定不对还请见谅. 以下代码是THREE.JS 源码文件中Scenes/FogExp2.js文件的注释. 更多更新在 : https://git