Table of Contents
yii2源码学习笔记(十一),yii2源码学习笔记
Home php教程 php手册 yii2源码学习笔记(十一),yii2源码学习笔记

yii2源码学习笔记(十一),yii2源码学习笔记

Jun 13, 2016 am 08:38 AM
controller yii2 study controller yes Source code of notes kind

yii2源码学习笔记(十一),yii2源码学习笔记

Controller控制器类,是所有控制器的基类,用于调用模型和布局。

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

 

yii2\base\Controller.php

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to delete Xiaohongshu notes How to delete Xiaohongshu notes Mar 21, 2024 pm 08:12 PM

How to delete Xiaohongshu notes? Notes can be edited in the Xiaohongshu APP. Most users don’t know how to delete Xiaohongshu notes. Next, the editor brings users pictures and texts on how to delete Xiaohongshu notes. Tutorial, interested users come and take a look! Xiaohongshu usage tutorial How to delete Xiaohongshu notes 1. First open the Xiaohongshu APP and enter the main page, select [Me] in the lower right corner to enter the special area; 2. Then in the My area, click on the note page shown in the picture below , select the note you want to delete; 3. Enter the note page, click [three dots] in the upper right corner; 4. Finally, the function bar will expand at the bottom, click [Delete] to complete.

What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? Mar 21, 2024 pm 09:30 PM

As a Xiaohongshu user, we have all encountered the situation where published notes suddenly disappeared, which is undoubtedly confusing and worrying. In this case, what should we do? This article will focus on the topic of &quot;What to do if the notes published by Xiaohongshu are missing&quot; and give you a detailed answer. 1. What should I do if the notes published by Xiaohongshu are missing? First, don't panic. If you find that your notes are missing, staying calm is key and don't panic. This may be caused by platform system failure or operational errors. Checking release records is easy. Just open the Xiaohongshu App and click &quot;Me&quot; → &quot;Publish&quot; → &quot;All Publications&quot; to view your own publishing records. Here you can easily find previously published notes. 3.Repost. If found

How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu Mar 12, 2024 am 10:40 AM

How to add product links in notes in Xiaohongshu? In the Xiaohongshu app, users can not only browse various contents but also shop, so there is a lot of content about shopping recommendations and good product sharing in this app. If If you are an expert on this app, you can also share some shopping experiences, find merchants for cooperation, add links in notes, etc. Many people are willing to use this app for shopping, because it is not only convenient, but also has many Experts will make some recommendations. You can browse interesting content and see if there are any clothing products that suit you. Let’s take a look at how to add product links to notes! How to add product links to Xiaohongshu Notes Open the app on the desktop of your mobile phone. Click on the app homepage

Learning Laravel from scratch: Detailed explanation of controller method invocation Learning Laravel from scratch: Detailed explanation of controller method invocation Mar 10, 2024 pm 05:03 PM

Learning Laravel from scratch: Detailed explanation of controller method invocation In the development of Laravel, controller is a very important concept. The controller serves as a bridge between the model and the view, responsible for processing requests from routes and returning corresponding data to the view for display. Methods in controllers can be called by routes. This article will introduce in detail how to write and call methods in controllers, and will provide specific code examples. First, we need to create a controller. You can use the Artisan command line tool to create

Revealing the appeal of C language: Uncovering the potential of programmers Revealing the appeal of C language: Uncovering the potential of programmers Feb 24, 2024 pm 11:21 PM

The Charm of Learning C Language: Unlocking the Potential of Programmers With the continuous development of technology, computer programming has become a field that has attracted much attention. Among many programming languages, C language has always been loved by programmers. Its simplicity, efficiency and wide application make learning C language the first step for many people to enter the field of programming. This article will discuss the charm of learning C language and how to unlock the potential of programmers by learning C language. First of all, the charm of learning C language lies in its simplicity. Compared with other programming languages, C language

Let's learn how to input the root number in Word together Let's learn how to input the root number in Word together Mar 19, 2024 pm 08:52 PM

When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:

How to publish notes tutorial on Xiaohongshu? Can it block people by posting notes? How to publish notes tutorial on Xiaohongshu? Can it block people by posting notes? Mar 25, 2024 pm 03:20 PM

As a lifestyle sharing platform, Xiaohongshu covers notes in various fields such as food, travel, and beauty. Many users want to share their notes on Xiaohongshu but don’t know how to do it. In this article, we will detail the process of posting notes on Xiaohongshu and explore how to block specific users on the platform. 1. How to publish notes tutorial on Xiaohongshu? 1. Register and log in: First, you need to download the Xiaohongshu APP on your mobile phone and complete the registration and login. It is very important to complete your personal information in the personal center. By uploading your avatar, filling in your nickname and personal introduction, you can make it easier for other users to understand your information, and also help them pay better attention to your notes. 3. Select the publishing channel: At the bottom of the homepage, click the &quot;Send Notes&quot; button and select the channel you want to publish.

Learn the main function in Go language from scratch Learn the main function in Go language from scratch Mar 27, 2024 pm 05:03 PM

Title: Learn the main function in Go language from scratch. As a simple and efficient programming language, Go language is favored by developers. In the Go language, the main function is an entry function, and every Go program must contain the main function as the entry point of the program. This article will introduce how to learn the main function in Go language from scratch and provide specific code examples. 1. First, we need to install the Go language development environment. You can go to the official website (https://golang.org

See all articles