热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

yii2源码学习笔记(十一)

yii2源码学习笔记(十一)
Controller控制器类,是所有控制器的基类,用于调用模型和布局。

  1 php
  2 /**
  3  * @link http://www.yiiframework.com/
  4  * @copyright Copyright (c) 2008 Yii Software LLC
  5  * @license http://www.yiiframework.com/license/
  6  */
  7 
  8 namespace yii\base;
  9 
 10 use Yii;
 11 
 12 /**
 13  * Controller is the base class for classes containing controller logic.
 14  *  控制器,是所用控制器类的基类
 15  * @property Module[] $modules All ancestor modules that this controller is located within. This property is
 16  * read-only.只读属性  当前控制器的所有模块
 17  * @property string $route The route (module ID, controller ID and action ID) of the current request. This
 18  * property is read-only.当前请求的路径  只读属性 可以获取到请求的路径
 19  * @property string $uniqueId The controller ID that is prefixed with the module ID (if any). This property is
 20  * read-only.为前缀的controller ID  唯一标识
 21  * @property View|\yii\web\View $view The view object that can be used to render views or view files.
 22  * 视图用来传递视图或视图文件.
 23  * @property string $viewPath The directory containing the view files for this controller. This property is
 24  * read-only. 包含当前控制器的视图目录
 25  *
 26  * @author Qiang Xue 
 27  * @since 2.0
 28  */
 29 class Controller extends Component implements ViewContextInterface
 30 {
 31     /**
 32      * @event ActionEvent an event raised right before executing a controller action.
 33      * ActionEvent事件提出正确的执行器动作之前执行。
 34      * You may set [[ActionEvent::isValid]] to be false to cancel the action execution.
 35      * 如果对事件的isValid属性设置为false,将取消action的执行
 36      */
 37     const EVENT_BEFORE_ACTION = 'beforeAction';
 38     /**
 39      * @event ActionEvent an event raised right after executing a controller action.
 40      * 在执行controller操作后触发的事件
 41      */
 42     const EVENT_AFTER_ACTION = 'afterAction';
 43 
 44     /**
 45      * @var string the ID of this controller.
 46      * 控制器id
 47      */
 48     public $id;
 49     /**
 50      * @var Module $module the module that this controller belongs to.
 51      * 所属模块
 52      */
 53     public $module;
 54     /**
 55      * @var string the ID of the action that is used when the action ID is not specified
 56      * in the request. Defaults to 'index'.控制器中默认动作,默认为index
 57      */
 58     public $defaultAction = 'index';
 59     /**
 60      * @var string|boolean the name of the layout to be applied to this controller's views.
 61      * 布局的名称 应用到该控制器的视图。
 62      * This property mainly affects the behavior of [[render()]].此属性主要影响[[render()]]行为
 63      * Defaults to null, meaning the actual layout value should inherit that from [[module]]'s layout value.
 64      * If false, no layout will be applied. 
 65      * 如果设置为false,则不使用布局文件
 66      */
 67     public $layout;
 68     /**
 69      * @var Action the action that is currently being executed. This property will be set
 70      * by [[run()]] when it is called by [[Application]] to run an action.
 71      * 当前执行的操作,可在事件中根据这个action来执行不同的操作
 72      */
 73     public $action;
 74 
 75     /**
 76      * @var View the view object that can be used to render views or view files.
 77      * 视图对象,用来定义输出的视图文件
 78      */
 79     private $_view;
 80 
 81 
 82     /**
 83      * @param string $id the ID of this controller.控制器的ID
 84      * @param Module $module the module that this controller belongs to.控制器的模块
 85      * @param array $config name-value pairs that will be used to initialize the object properties.
 86      * 初始化对像时的配置文件
 87      */
 88     public function __construct($id, $module, $cOnfig= [])
 89     {
 90         //初始化控制器id,模块,根据配置文件初始化控制器对象
 91         $this->id = $id;
 92         $this->module = $module;
 93         parent::__construct($config);
 94     }
 95 
 96     /**
 97      * Declares external actions for the controller.定义action声明控制器的外部操作
 98      * This method is meant to be overwritten to declare external actions for the controller.
 99      * It should return an array, with array keys being action IDs, and array values the corresponding
100      * action class names or action configuration arrays. For example,
101      * 这个方法指定独立的action,返回格式为数组,name为action的id,value为action类的实现,
102      * ~~~
103      * return [
104      *     'action1' => 'app\components\Action1',
105      *     'action2' => [
106      *         'class' => 'app\components\Action2',
107      *         'property1' => 'value1',
108      *         'property2' => 'value2',
109      *     ],
110      * ];
111      * ~~~
112      *
113      * [[\Yii::createObject()]] will be used later to create the requested action
114      * using the configuration provided here.
115      * 使用此处提供的配置来创建请求的操作。
116      */
117     public function actions()
118     {
119         return [];
120     }
121 
122     /**
123      * Runs an action within this controller with the specified action ID and parameters.
124      * 控制器中运行指定的操作标识和参数。
125      * If the action ID is empty, the method will use [[defaultAction]].
126      * 如果没有定义ID,会调用默认操作
127      * @param string $id the ID of the action to be executed. 要执行的动作标识。
128      * @param array $params the parameters (name-value pairs) to be passed to the action.
129      * 传递给操作的参数。
130      * @return mixed the result of the action.  操作结果
131      * @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully.
132      * @see createAction()
133      */
134     public function runAction($id, $params = [])
135     {
136         $action = $this->createAction($id);//创建操作
137         if ($action === null) {//创建失败,抛出异常
138             throw new InvalidRouteException('Unable to resolve the request: ' . $this->getUniqueId() . '/' . $id);
139         }
140 
141         Yii::trace("Route to run: " . $action->getUniqueId(), __METHOD__);
142 
143         if (Yii::$app->requestedAction === null) {
144             // 记录当前的操作为requestedAction
145             Yii::$app->requestedAction = $action;
146         }
147 
148         $oldAction = $this->action;//将操作中的信息保存
149         $this->action = $action;//写入属性
150         //保存当前控制器的所有父模块
151         $modules = [];
152         $runAction = true;
153 
154         // call beforeAction on modules 从外到里一层层执行module的beforeAction
155         foreach ($this->getModules() as $module) {
156             if ($module->beforeAction($action)) {
157                  // 将执行成功的module放入到$modules中,顺序会颠倒
158                 array_unshift($modules, $module);
159             } else {
160                  // 执行失败,就标记一下
161                 $runAction = false;
162                 break;
163             }
164         }
165 
166         $result = null;
167 
168         if ($runAction && $this->beforeAction($action)) {
169             // run the action 执行成功就执行action
170             $result = $action->runWithParams($params);
171             // 执行controller本身的afterAction
172             $result = $this->afterAction($action, $result);
173 
174             // call afterAction on modules 从里到外一层层执行所有
175             foreach ($modules as $module) {
176                 /* @var $module Module */
177                 $result = $module->afterAction($action, $result);
178             }
179         }
180 
181         $this->action = $oldAction;
182 
183         return $result;
184     }

yii2\base\Controller.php


推荐阅读
  • 本文详细介绍了SQL日志收缩的方法,包括截断日志和删除不需要的旧日志记录。通过备份日志和使用DBCC SHRINKFILE命令可以实现日志的收缩。同时,还介绍了截断日志的原理和注意事项,包括不能截断事务日志的活动部分和MinLSN的确定方法。通过本文的方法,可以有效减小逻辑日志的大小,提高数据库的性能。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • 关羽败走麦城时路过马超封地 马超为何没有出手救人
    对当年关羽败走麦城,恰好路过马超的封地,为啥马超不救他?很感兴趣的小伙伴们,趣历史小编带来详细的文章供大家参考。说到英雄好汉,便要提到一本名著了,没错,那就是《三国演义》。书中虽 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • PHP设置MySQL字符集的方法及使用mysqli_set_charset函数
    本文介绍了PHP设置MySQL字符集的方法,详细介绍了使用mysqli_set_charset函数来规定与数据库服务器进行数据传送时要使用的字符集。通过示例代码演示了如何设置默认客户端字符集。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 橱窗设计的表现手法及其应用
    本文介绍了橱窗设计的表现手法,包括直接展示、寓意与联想、夸张与幽默等。通过对商品的折、拉、叠、挂、堆等陈列技巧,橱窗设计能够充分展现商品的形态、质地、色彩、样式等特性。同时,寓意与联想可以通过象形形式或抽象几何道具来唤起消费者的联想与共鸣,创造出强烈的时代气息和视觉空间。合理的夸张和贴切的幽默能够明显夸大商品的美的因素,给人以新颖奇特的心理感受,引起人们的笑声和思考。通过这些表现手法,橱窗设计能够有效地传达商品的个性内涵,吸引消费者的注意力。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • faceu激萌变老特效的使用方法详解
    本文介绍了faceu激萌变老特效的使用方法,包括打开faceu激萌app、点击贴纸、选择热门贴纸中的变老特效,然后对准人脸进行拍摄,即可给照片添加变老特效。操作简单,适合新用户使用。 ... [详细]
  • Android中高级面试必知必会,积累总结
    本文介绍了Android中高级面试的必知必会内容,并总结了相关经验。文章指出,如今的Android市场对开发人员的要求更高,需要更专业的人才。同时,文章还给出了针对Android岗位的职责和要求,并提供了简历突出的建议。 ... [详细]
  • 大连微软技术社区举办《.net core始于足下》活动,获得微软赛百味和易迪斯的赞助
    九月十五日,大连微软技术社区举办了《.net core始于足下》活动,共有51人报名参加,实际到场人数为43人,还有一位专程从北京赶来的同学。活动得到了微软赛百味和易迪斯的赞助,场地也由易迪斯提供。活动中大家积极交流,取得了非常成功的效果。 ... [详细]
  • 给定一个二叉树,要求随机选择树上的一个节点。解法:遍历树的过程中,随机选择一个节点即可。具体做法参看:从输入 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
author-avatar
寡凫lo单鹄官方
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有