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

yii2源码学习笔记(十四)

yii2源码学习笔记(十四)
Module类是模块和应用类的基类。  yiisoft\yii2\base\Module.php

  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 use yii\di\ServiceLocator;
 12 
 13 /**
 14  * Module is the base class for module and application classes.
 15  *  Module是模块和应用类的基类
 16  * A module represents a sub-application which contains MVC elements by itself, such as
 17  * models, views, controllers, etc.
 18  * 模块是一个由模型、视图、控制器等组成的子应用
 19  * A module may consist of [[modules|sub-modules]].
 20  * 模块内也可以包含模块或子模块
 21  * [[components|Components]] may be registered with the module so that they are globally
 22  * accessible within the module.
 23  * 组件可以注册到模块,以便在模块内全局访问
 24  * @property array $aliases List of path aliases to be defined. The array keys are alias names (must start
 25  * with '@') and the array values are the corresponding paths or aliases. See [[setAliases()]] for an example.
 26  * This property is write-only. 要定义的别名路径数组    只写
 27  * @property string $basePath The root directory of the module. 模块的根路径
 28  * @property string $controllerPath The directory that contains the controller classes. This property is
 29  * read-only.   控制器类的路径 只读
 30  * @property string $layoutPath The root directory of layout files. Defaults to "[[viewPath]]/layouts".
 31  * 模板路径数组 只读
 32  * @property array $modules The modules (indexed by their IDs). 模块数组
 33  * @property string $uniqueId The unique ID of the module. This property is read-only.模块的唯一标识 只读
 34  * @property string $viewPath The root directory of view files. Defaults to "[[basePath]]/views".
 35  * 模块下视图文件路径
 36  * @author Qiang Xue 
 37  * @since 2.0
 38  */
 39 class Module extends ServiceLocator
 40 {
 41     /**
 42      * @event ActionEvent an event raised before executing a controller action. 在执行控制的的action方法前触发
 43      * You may set [[ActionEvent::isValid]] to be false to cancel the action execution.
 44      * 可以设置[[ActionEvent::isValid]]为false取消行动的执行。
 45      */
 46     const EVENT_BEFORE_ACTION = 'beforeAction';
 47     /**
 48      * @event ActionEvent an event raised after executing a controller action.
 49      * 在执行控制的的action方法后触发
 50      * 
 51      */
 52     const EVENT_AFTER_ACTION = 'afterAction';
 53 
 54     /**
 55      * @var array custom module parameters (name => value). 自定义模块参数
 56      */
 57     public $params = [];
 58     /**
 59      * @var string an ID that uniquely identifies this module among other modules which have the same [[module|parent]].
 60      * 模块的唯一标识,用于区分同一父模块下的模块
 61      */
 62     public $id;
 63     /**
 64      * @var Module the parent module of this module. Null if this module does not have a parent.
 65      *  当前模块的父模块
 66      */
 67     public $module;
 68     /**
 69      * @var string|boolean the layout that should be applied for views within this module. This refers to a view name
 70      * relative to [[layoutPath]]. If this is not set, it means the layout value of the [[module|parent module]]
 71      * will be taken. If this is false, layout will be disabled within this module.
 72      * 布局文件 如果没有设置,调用 [[module|parent module]]的值。如果是false,在模块中布局将被禁用。
 73      */
 74     public $layout;
 75     /**
 76      * @var array mapping from controller ID to controller configurations. 控制器ID到控制器配置的映射
 77      * Each name-value pair specifies the configuration of a single controller.
 78      * A controller configuration can be either a string or an array.
 79      * If the former, the string should be the fully qualified class name of the controller.
 80      * If the latter, the array must contain a 'class' element which specifies
 81      * the controller's fully qualified class name, and the rest of the name-value pairs
 82      * in the array are used to initialize the corresponding controller properties. For example,
 83      * 每个键值对指定单独的控制器,控制器配置可以是字符串或者数组,如果是前者,该字符串是指定控制的的全路径
 84  95  * 如果是后者,则包含一个‘class’元素指定控制器的全路径,其余的参数用于初始化对应的属性
 85      * ~~~
 86      * [
 87      *   'account' => 'app\controllers\UserController',
 88      *   'article' => [
 89      *      'class' => 'app\controllers\PostController',
 90      *      'pageTitle' => 'something new',
 91      *   ],
 92      * ]
 93      * ~~~
 94      */
 95     public $cOntrollerMap= [];
 96     /**
 97      * @var string the namespace that controller classes are in.    控制器的命名空间
 98      * This namespace will be used to load controller classes by prepending it to the controller
 99      * class name.
100      * 命名空间 在控制器类的前面加载控制器类
101      * If not set, it will use the `controllers` sub-namespace under the namespace of this module.
102      * For example, if the namespace of this module is "foo\bar", then the default
103      * controller namespace would be "foo\bar\controllers".
104      * 如果没有设置,默认为当前模块的命名空间加上 `controllers`构成的命名空间
105 119  * 如当前模块的命名空间为"foo\bar",控制器的默认命名空间为"foo\bar\controllers"
106      * See also the [guide section on autoloading](guide:concept-autoloading) to learn more about
107      * defining namespaces and how classes are loaded.
108      */
109     public $controllerNamespace;
110     /**
111      * @var string the default route of this module. Defaults to 'default'. 当前前模块的默认路由
112      * The route may consist of child module ID, controller ID, and/or action ID.
113      * For example, `help`, `post/create`, `admin/post/create`.
114      * If action ID is not given, it will take the default value as specified in
115      * [[Controller::defaultAction]].
116      * route 可能包含子模块ID,控制器ID,操作ID,如果action ID未给定,会调用[Controller::defaultAction]指定的action
117      */
118     public $defaultRoute = 'default';
119 
120     /**
121      * @var string the root directory of the module.    当前模块的根路径
122      */
123     private $_basePath;
124     /**
125      * @var string the root directory that contains view files for this module 当前模块下视图文件的路径
126      */
127     private $_viewPath;
128     /**
129      * @var string the root directory that contains layout view files for this module.
130      * 当前模块下的布局文件路径
131      */
132     private $_layoutPath;
133     /**
134      * @var array child modules of this module  当前模块的子模块数组
135      */
136     private $_modules = [];
137 
138 
139     /**
140      * Constructor. 构造函数
141      * @param string $id the ID of this module 当前模块的标识
142      * @param Module $parent the parent module (if any) 当前模块的父模块
143      * @param array $config name-value pairs that will be used to initialize the object properties
144      * 配置文件 用于初始化对象属性
145      */
146     public function __construct($id, $parent = null, $cOnfig= [])
147     {
148         $this->id = $id; //给当前模块唯一标识
149         $this->module = $parent;    //当前模块的父模块
150         parent::__construct($config);   //调用父类的配置
151     }
152 
153     /**
154      * Returns the currently requested instance of this module class.   取得当前类的实例
155      * If the module class is not currently requested, null will be returned.
156      * 没有当前请求的模块类,将返回null。
157      * This method is provided so that you access the module instance from anywhere within the module.
158      * 可以在模块内的任何地方访问类的实例
159      * @return static|null the currently requested instance of this module class, or null if the module class is not requested.
160      */
161     public static function getInstance()
162     {
163         $class = get_called_class();
164         return isset(Yii::$app->loadedModules[$class]) ? Yii::$app->loadedModules[$class] : null;
165     }
166 
167     /**
168      * Sets the currently requested instance of this module class.  设置模块类的当前请求实例。
169      * @param Module|null $instance the currently requested instance of this module class.
170      * If it is null, the instance of the calling class will be removed, if any.
171      * 当前模块类的实例。如果为null,调用类的实例将被删除
172      */
173     public static function setInstance($instance)
174     {
175         if ($instance === null) {//如果没有传入参数,直接unset
176             unset(Yii::$app->loadedModules[get_called_class()]);
177         } else {//将该类和类的实例存入loadedModules数组中
178             Yii::$app->loadedModules[get_class($instance)] = $instance;
179         }
180     }
181 
182     /**
183      * Initializes the module.
184      * 初始化模块
185      * This method is called after the module is created and initialized with property values
186      * given in configuration. The default implementation will initialize [[controllerNamespace]]
187      * if it is not set.
188      * 该模块创建和初始化给出的配置  如果没有设置,默认初始化[[controllerNamespace]]
189      * If you override this method, please make sure you call the parent implementation.
190      * 重写确保父类调用
191      */
192     public function init()
193     {
194         if ($this->cOntrollerNamespace=== null) {//判断是否为空
195             $class = get_class($this); //获取类名
196             if (($pos = strrpos($class, '\\')) !== false) {
197                 $this->cOntrollerNamespace= substr($class, 0, $pos) . '\\controllers'; //取得命名空间
198             }
199         }
200     }
201 
202     /**
203      * Returns an ID that uniquely identifies this module among all modules within the current application.
204      * Note that if the module is an application, an empty string will be returned.
205      * 当前应用程序中模块的唯一标识,如果该模块是应用程序返回空字符串
206      * @return string the unique ID of the module.模块的唯一标识
207      */
208     public function getUniqueId()
209     {     //如果当前模块有父模块,则返回拼接的标识作为唯一ID,否则只返回当前模块ID
210         return $this->module ? ltrim($this->module->getUniqueId() . '/' . $this->id, '/') : $this->id;
211     }
212 
213     /**
214      * Returns the root directory of the module.    返回当前模块的根路径
215      * It defaults to the directory containing the module class file.   默认为包含模块类文件的路径。
216      * @return string the root directory of the module. 当前模块的根路径
217      */
218     public function getBasePath()
219     {
220         if ($this->_basePath === null) {
221             $class = new \ReflectionClass($this);   //生成当前类的反射对象
222             $this->_basePath = dirname($class->getFileName());//取得类定义的路径
223         }
224 
225         return $this->_basePath;
226     }
227 
228     /**
229      * Sets the root directory of the module.   设置当前模块的根路径
230      * This method can only be invoked at the beginning of the constructor. 只在构造函数开始时调用。
231      * @param string $path the root directory of the module. This can be either a directory name or a path alias.
232      * 模块的根目录。可以是一个目录名或路径别名
233      * @throws InvalidParamException if the directory does not exist. 如果路径不存在。抛出异常
234      */
235     public function setBasePath($path)
236     {
237         $path = Yii::getAlias($path);//将路径别名转换为实际路径。
238         $p = realpath($path);   //返回绝对路径名
239         if ($p !== false && is_dir($p)) {
240             $this->_basePath = $p;//是目录名且不为false,返回目录名,否则抛出异常
241         } else {
242             throw new InvalidParamException("The directory does not exist: $path");
243         }
244     }


推荐阅读
  • MVC设计模式的介绍和演化过程
    本文介绍了MVC设计模式的基本概念和原理,以及在实际项目中的演化过程。通过分离视图、模型和控制器,实现了代码的解耦和重用,提高了项目的可维护性和可扩展性。详细讲解了分离视图、分离模型和分离控制器的具体步骤和规则,以及它们在项目中的应用。同时,还介绍了基础模型的封装和控制器的命名规则。该文章适合对MVC设计模式感兴趣的读者阅读和学习。 ... [详细]
  • MySQL中的MVVC多版本并发控制机制的应用及实现
    本文介绍了MySQL中MVCC的应用及实现机制。MVCC是一种提高并发性能的技术,通过对事务内读取的内存进行处理,避免写操作堵塞读操作的并发问题。与其他数据库系统的MVCC实现机制不尽相同,MySQL的MVCC是在undolog中实现的。通过undolog可以找回数据的历史版本,提供给用户读取或在回滚时覆盖数据页上的数据。MySQL的大多数事务型存储引擎都实现了MVCC,但各自的实现机制有所不同。 ... [详细]
  • Todayatworksomeonetriedtoconvincemethat:今天在工作中有人试图说服我:{$obj->getTableInfo()}isfine ... [详细]
  • 本文内容为asp.net微信公众平台开发的目录汇总,包括数据库设计、多层架构框架搭建和入口实现、微信消息封装及反射赋值、关注事件、用户记录、回复文本消息、图文消息、服务搭建(接入)、自定义菜单等。同时提供了示例代码和相关的后台管理功能。内容涵盖了多个方面,适合综合运用。 ... [详细]
  • 本文介绍了Sencha Touch的学习使用心得,主要包括搭建项目框架的过程。作者强调了使用MVC模式的重要性,并提供了一个干净的引用示例。文章还介绍了Index.html页面的作用,以及如何通过链接样式表来改变全局风格。 ... [详细]
  • 本文介绍了MVP架构模式及其在国庆技术博客中的应用。MVP架构模式是一种演变自MVC架构的新模式,其中View和Model之间的通信通过Presenter进行。相比MVC架构,MVP架构将交互逻辑放在Presenter内部,而View直接从Model中读取数据而不是通过Controller。本文还探讨了MVP架构在国庆技术博客中的具体应用。 ... [详细]
  • 从零基础到精通的前台学习路线
    随着互联网的发展,前台开发工程师成为市场上非常抢手的人才。本文介绍了从零基础到精通前台开发的学习路线,包括学习HTML、CSS、JavaScript等基础知识和常用工具的使用。通过循序渐进的学习,可以掌握前台开发的基本技能,并有能力找到一份月薪8000以上的工作。 ... [详细]
  • Asp.net Mvc Framework 七 (Filter及其执行顺序) 的应用示例
    本文介绍了在Asp.net Mvc中应用Filter功能进行登录判断、用户权限控制、输出缓存、防盗链、防蜘蛛、本地化设置等操作的示例,并解释了Filter的执行顺序。通过示例代码,详细说明了如何使用Filter来实现这些功能。 ... [详细]
  • 本文介绍了ASP.NET Core MVC的入门及基础使用教程,根据微软的文档学习,建议阅读英文文档以便更好理解,微软的工具化使用方便且开发速度快。通过vs2017新建项目,可以创建一个基础的ASP.NET网站,也可以实现动态网站开发。ASP.NET MVC框架及其工具简化了开发过程,包括建立业务的数据模型和控制器等步骤。 ... [详细]
  • MySQL数据库锁机制及其应用(数据库锁的概念)
    本文介绍了MySQL数据库锁机制及其应用。数据库锁是计算机协调多个进程或线程并发访问某一资源的机制,在数据库中,数据是一种供许多用户共享的资源,如何保证数据并发访问的一致性和有效性是数据库必须解决的问题。MySQL的锁机制相对简单,不同的存储引擎支持不同的锁机制,主要包括表级锁、行级锁和页面锁。本文详细介绍了MySQL表级锁的锁模式和特点,以及行级锁和页面锁的特点和应用场景。同时还讨论了锁冲突对数据库并发访问性能的影响。 ... [详细]
  • 本文讲述了作者从最初对软件工程的选择迷茫到逐渐喜欢并坚持学习的经历。作者在大学期间通过学习专业课和参与项目开发,不断挑战自己并取得成就感。虽然曾考虑过转专业和复读,但最终决定坚持学习软件工程,并为自己的未来努力奋斗。作者还提到了大学生活与自己最初的预期不同,但对此并没有太多抱怨。 ... [详细]
  • 使用J2SE模拟MVC模式开发桌面应用程序的工程包的介绍
    以我开发过的一个娱乐管理系统为例:下图为我系统的业务逻辑的MVC流程:下图为以Eclipse开发中各包的说明:转载于:https:blog ... [详细]
  • wpf+mvvm代码组织结构及实现方式
    本文介绍了wpf+mvvm代码组织结构的由来和实现方式。作者回顾了自己大学时期接触wpf开发和mvvm模式的经历,认为mvvm模式使得开发更加专注于业务且高效。与此同时,作者指出mvvm模式相较于mvc模式的优势。文章还提到了当没有mvvm时处理数据和UI交互的例子,以及前后端分离和组件化的概念。作者希望能够只关注原始数据结构,将数据交给UI自行改变,从而解放劳动力,避免加班。 ... [详细]
  • 本文讨论了在shiro java配置中加入Shiro listener后启动失败的问题。作者引入了一系列jar包,并在web.xml中配置了相关内容,但启动后却无法正常运行。文章提供了具体引入的jar包和web.xml的配置内容,并指出可能的错误原因。该问题可能与jar包版本不兼容、web.xml配置错误等有关。 ... [详细]
  • 本文讨论了在ASP中创建RazorFunctions.cshtml文件时出现的问题,即ASP.global_asax不存在于命名空间ASP中。文章提供了解决该问题的代码示例,并详细解释了代码中涉及的关键概念,如HttpContext、Request和RouteData等。通过阅读本文,读者可以了解如何解决该问题并理解相关的ASP概念。 ... [详细]
author-avatar
文艺范老大叔
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有