没有命名空间的类 yii yii\base yii\behaviors yii\caching yii\captcha yii\console yii\console\controllers yii\console\widgets yii\data yii\db yii\db\conditions yii\db\cubrid yii\db\cubrid\conditions yii\db\mssql yii\db\mssql\conditions yii\db\mysql yii\db\oci yii\db\oci\conditions yii\db\pgsql yii\db\sqlite yii\db\sqlite\conditions yii\di yii\filters yii\filters\auth yii\grid yii\helpers yii\i18n yii\log yii\mail yii\mutex yii\rbac yii\rest yii\test yii\validators yii\web yii\widgets

Class yii\base\Module

继承yii\base\Module » yii\di\ServiceLocator » yii\base\Component » yii\base\BaseObject
实现yii\base\Configurable
子类yii\base\Application, yii\console\Application, yii\web\Application
可用版本自2.0
源码 https://github.com/yiichina/yii2/blob/api/framework/base/Module.php

Module is the base class for module and application classes.

A module represents a sub-application which contains MVC elements by itself, such as models, views, controllers, etc.

A module may consist of sub-modules.

Components may be registered with the module so that they are globally accessible within the module.

For more details and usage information on Module, see the guide article on modules.

公共属性

隐藏继承的属性

属性类型描述被定义在
$aliases array List of path aliases to be defined. yii\base\Module
$basePath string The root directory of the module. yii\base\Module
$behaviors yii\base\Behavior[] List of behaviors attached to this component yii\base\Component
$components array The 组件定义列表或加载的组件实例(ID => definition or instance)。 yii\di\ServiceLocator
$controllerMap array Mapping from controller ID to controller configurations. yii\base\Module
$controllerNamespace string The namespace that controller classes are in. yii\base\Module
$controllerPath string The directory that contains the controller classes. yii\base\Module
$defaultRoute string The default route of this module. yii\base\Module
$id string An ID that uniquely identifies this module among other modules which have the same parent. yii\base\Module
$layout string|boolean The layout that should be applied for views within this module. yii\base\Module
$layoutPath string The root directory of layout files. yii\base\Module
$module yii\base\Module The parent module of this module. yii\base\Module
$modules array The modules (indexed by their IDs). yii\base\Module
$params array Custom module parameters (name => value). yii\base\Module
$uniqueId string The unique ID of the module. yii\base\Module
$version string The version of this module. yii\base\Module
$viewPath string The root directory of view files. yii\base\Module

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. yii\base\Component
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\Module
__get() Getter 魔术方法。 重写此方法以支持访问诸如读取属性之类的组件。 yii\di\ServiceLocator
__isset() 检查属性值是否为 null。 此方法通过检查是否已加载命名组件来覆盖父类的实现。 yii\di\ServiceLocator
__set() Sets the value of a component property. yii\base\Component
__unset() Sets a component property to be null. yii\base\Component
afterAction() This method is invoked right after an action within this module is executed. yii\base\Module
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
beforeAction() This method is invoked right before an action within this module is executed. yii\base\Module
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Component
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Component
className() Returns the fully qualified name of this class. yii\base\BaseObject
clear() 从定位器移除组件。 yii\di\ServiceLocator
createController() Creates a controller instance based on the given route. yii\base\Module
createControllerByID() Creates a controller based on the given controller ID. yii\base\Module
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
get() 返回具有指定 ID 的组件实例。 yii\base\Module
getBasePath() Returns the root directory of the module. yii\base\Module
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getComponents() 返回组件定义列表或已加载的组件实例。 yii\di\ServiceLocator
getControllerPath() Returns the directory that contains the controller classes according to $controllerNamespace. yii\base\Module
getInstance() Returns the currently requested instance of this module class. yii\base\Module
getLayoutPath() Returns the directory that contains layout view files for this module. yii\base\Module
getModule() Retrieves the child module of the specified ID. yii\base\Module
getModules() Returns the sub-modules in this module. yii\base\Module
getUniqueId() Returns an ID that uniquely identifies this module among all modules within the current application. yii\base\Module
getVersion() Returns current module version. yii\base\Module
getViewPath() Returns the directory that contains the view files for this module. yii\base\Module
has() 返回一个值,该值表示定位器是否具有指定的组件定义或是否已实例化该组件。 此方法根据 $checkInstance 的值返回不同的结果。 yii\base\Module
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\Component
hasModule() Checks whether the child module of the specified ID exists. yii\base\Module
hasProperty() Returns a value indicating whether a property is defined for this component. yii\base\Component
init() Initializes the module. yii\base\Module
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
runAction() Runs a controller action specified by a route. yii\base\Module
set() 用定位器注册一个组件。 yii\di\ServiceLocator
setAliases() Defines path aliases. yii\base\Module
setBasePath() Sets the root directory of the module. yii\base\Module
setComponents() 在定位器中注册一组组件定义。 yii\di\ServiceLocator
setInstance() Sets the currently requested instance of this module class. yii\base\Module
setLayoutPath() Sets the directory that contains the layout files. yii\base\Module
setModule() Adds a sub-module to this module. yii\base\Module
setModules() Registers sub-modules in the current module. yii\base\Module
setVersion() Sets current module version. yii\base\Module
setViewPath() Sets the directory that contains the view files. yii\base\Module
trigger() Triggers an event. yii\base\Component

受保护的方法

隐藏继承的方法

方法描述被定义在
defaultVersion() Returns default module version. yii\base\Module

Events

隐藏继承的事件

事件类型描述被定义在
EVENT_AFTER_ACTION yii\base\ActionEvent An event raised after executing a controller action. yii\base\Module
EVENT_BEFORE_ACTION yii\base\ActionEvent An event raised before executing a controller action. yii\base\Module

属性详情

$aliases 公共 只写 属性

List of path aliases to be defined. The array keys are alias names (must start with @) and the array values are the corresponding paths or aliases. For example,

[
    '@models' => '@app/models', // an existing alias
    '@backend' => __DIR__ . '/../backend',  // a directory
]
public void setAliases ( $aliases )
$basePath 公共 属性

The root directory of the module.

public string getBasePath ( )
public void setBasePath ( $path )
$controllerMap 公共 属性

Mapping from controller ID to controller configurations. Each name-value pair specifies the configuration of a single controller. A controller configuration can be either a string or an array. If the former, the string should be the fully qualified class name of the controller. If the latter, the array must contain a class element which specifies the controller's fully qualified class name, and the rest of the name-value pairs in the array are used to initialize the corresponding controller properties. For example,

[
  'account' => 'app\controllers\UserController',
  'article' => [
     'class' => 'app\controllers\PostController',
     'pageTitle' => 'something new',
  ],
]
public array $controllerMap = []
$controllerNamespace 公共 属性

The namespace that controller classes are in. This namespace will be used to load controller classes by prepending it to the controller class name.

If not set, it will use the controllers sub-namespace under the namespace of this module. For example, if the namespace of this module is foo\bar, then the default controller namespace would be foo\bar\controllers.

See also the guide section on autoloading to learn more about defining namespaces and how classes are loaded.

$controllerPath 公共 只读 属性

The directory that contains the controller classes.

$defaultRoute 公共 属性

The default route of this module. Defaults to default. The route may consist of child module ID, controller ID, and/or action ID. For example, help, post/create, admin/post/create. If action ID is not given, it will take the default value as specified in yii\base\Controller::$defaultAction.

public string $defaultRoute 'default'
$id 公共 属性

An ID that uniquely identifies this module among other modules which have the same parent.

public string $id null
$layout 公共 属性

The layout that should be applied for views within this module. This refers to a view name relative to $layoutPath. If this is not set, it means the layout value of the parent module will be taken. If this is false, layout will be disabled within this module.

public string|boolean $layout null
$layoutPath 公共 属性

The root directory of layout files. Defaults to "$viewPath/layouts".

public string getLayoutPath ( )
public void setLayoutPath ( $path )
$module 公共 属性

The parent module of this module. null if this module does not have a parent.

public yii\base\Module $module null
$modules 公共 属性

The modules (indexed by their IDs).

public array getModules ( $loadedOnly false )
public void setModules ( $modules )
$params 公共 属性

Custom module parameters (name => value).

public array $params = []
$uniqueId 公共 只读 属性

The unique ID of the module.

public string getUniqueId ( )
$version 公共 属性

The version of this module.

public string getVersion ( )
public void setVersion ( $version )
$viewPath 公共 属性

The root directory of view files. Defaults to "$basePath/views".

public string getViewPath ( )
public void setViewPath ( $path )

方法详情

__construct() 公共 方法

Constructor.

public void __construct($id, $parent null, $config = [])
$id string

The ID of this module.

$parent yii\base\Module

The parent module (if any).

$config array

Name-value pairs that will be used to initialize the object properties.

afterAction() 公共 方法

This method is invoked right after an action within this module is executed.

The method will trigger the EVENT_AFTER_ACTION event. The return value of the method will be used as the action return value.

If you override this method, your code should look like the following:

public function afterAction($action, $result)
{
    $result = parent::afterAction($action, $result);
    // your custom code here
    return $result;
}
public mixed afterAction($action, $result)
$action yii\base\Action

The action just executed.

$result mixed

The action return result.

return mixed

The processed action result.

beforeAction() 公共 方法

This method is invoked right before an action within this module is executed.

The method will trigger the EVENT_BEFORE_ACTION event. The return value of the method will determine whether the action should continue to run.

In case the action should not run, the request should be handled inside of the beforeAction code by either providing the necessary output or redirecting the request. Otherwise the response will be empty.

If you override this method, your code should look like the following:

public function beforeAction($action)
{
    if (!parent::beforeAction($action)) {
        return false;
    }

    // your custom code here

    return true; // or false to not run the action
}
public boolean beforeAction($action)
$action yii\base\Action

The action to be executed.

return boolean

Whether the action should continue to be executed.

createController() 公共 方法

Creates a controller instance based on the given route.

The route should be relative to this module. The method implements the following algorithm to resolve the given route:

  1. If the route is empty, use $defaultRoute;
  2. If the first segment of the route is a valid module ID as declared in $modules, call the module's createController() with the rest part of the route;
  3. If the first segment of the route is found in $controllerMap, create a controller based on the corresponding configuration found in $controllerMap;
  4. The given route is in the format of abc/def/xyz. Try either abc\DefController or abc\def\XyzController class within the controller namespace.

If any of the above steps resolves into a controller, it is returned together with the rest part of the route which will be treated as the action ID. Otherwise, false will be returned.

public array|boolean createController($route)
$route string

The route consisting of module, controller and action IDs.

return array|boolean

If the controller is created successfully, it will be returned together with the requested action ID. Otherwise false will be returned.

throws yii\base\InvalidConfigException

if the controller class and its file do not match.

createControllerByID() 公共 方法

Creates a controller based on the given controller ID.

The controller ID is relative to this module. The controller class should be namespaced under $controllerNamespace.

Note that this method does not check $modules or $controllerMap.

public yii\base\Controller|null createControllerByID($id)
$id string

The controller ID.

return yii\base\Controller|null

The newly created controller instance, or null if the controller ID is invalid.

throws yii\base\InvalidConfigException

if the controller class and its file name do not match. This exception is only thrown when in debug mode.

defaultVersion() 受保护 方法 (自版本 2.0.11 可用)

Returns default module version.

Child class may override this method to provide more specific version detection.

protected string defaultVersion()
return string

The version of this module.

get() 公共 方法

返回具有指定 ID 的组件实例。

Since version 2.0.13, if a component isn't defined in the module, it will be looked up in the parent module. The parent module may be the application.

public object|null get($id, $throwException true)
$id string

组件 ID(e.g. db)。

$throwException boolean

如果 $id 之前未在定位器中注册,是否抛出一个异常。

return object|null

指定 ID 的组件。如果 $throwException 为 false 并且 $id 在之前没有被注册,将会返回 null。

throws yii\base\InvalidConfigException

如果 $id 为不存在的组件 ID 抛出的异常。

getBasePath() 公共 方法

Returns the root directory of the module.

It defaults to the directory containing the module class file.

public string getBasePath()
return string

The root directory of the module.

getControllerPath() 公共 方法

Returns the directory that contains the controller classes according to $controllerNamespace.

Note that in order for this method to return a value, you must define an alias for the root namespace of $controllerNamespace.

public string getControllerPath()
return string

The directory that contains the controller classes.

throws yii\base\InvalidArgumentException

if there is no alias defined for the root namespace of $controllerNamespace.

getInstance() 公共 静态 方法

Returns the currently requested instance of this module class.

If the module class is not currently requested, null will be returned. This method is provided so that you access the module instance from anywhere within the module.

public static static|null getInstance()
return static|null

The currently requested instance of this module class, or null if the module class is not requested.

getLayoutPath() 公共 方法

Returns the directory that contains layout view files for this module.

public string getLayoutPath()
return string

The root directory of layout files. Defaults to "$viewPath/layouts".

getModule() 公共 方法

Retrieves the child module of the specified ID.

This method supports retrieving both child modules and grand child modules.

参见 hasModule().

public yii\base\Module|null getModule($id, $load true)
$id string

Module ID (case-sensitive). To retrieve grand child modules, use ID path relative to this module (e.g. admin/content).

$load boolean

Whether to load the module if it is not yet loaded.

return yii\base\Module|null

The module instance, null if the module does not exist.

getModules() 公共 方法

Returns the sub-modules in this module.

public array getModules($loadedOnly false)
$loadedOnly boolean

Whether to return the loaded sub-modules only. If this is set false, then all sub-modules registered in this module will be returned, whether they are loaded or not. Loaded modules will be returned as objects, while unloaded modules as configuration arrays.

return array

The modules (indexed by their IDs).

getUniqueId() 公共 方法

Returns an ID that uniquely identifies this module among all modules within the current application.

Note that if the module is an application, an empty string will be returned.

public string getUniqueId()
return string

The unique ID of the module.

getVersion() 公共 方法 (自版本 2.0.11 可用)

Returns current module version.

If version is not explicitly set, defaultVersion() method will be used to determine its value.

public string getVersion()
return string

The version of this module.

getViewPath() 公共 方法

Returns the directory that contains the view files for this module.

public string getViewPath()
return string

The root directory of view files. Defaults to "$basePath/views".

has() 公共 方法

返回一个值,该值表示定位器是否具有指定的组件定义或是否已实例化该组件。 此方法根据 $checkInstance 的值返回不同的结果。

Since version 2.0.13, if a component isn't defined in the module, it will be looked up in the parent module. The parent module may be the application.

  • 如果 $checkInstance 为 false(default), 此方法将返回一个表示定位器是否具有指定组件定义的值。
  • 如果 $checkInstance 为 true, 此方法会返回一个表示定位器是否已经实例化指定组件的值。
public boolean has($id, $checkInstance false)
$id string

组件 ID(e.g. db)。

$checkInstance boolean

是否应检查组件是否已共享和实例化。

return boolean

是否定位器具有指定的组件定义或已实例化组件。

hasModule() 公共 方法

Checks whether the child module of the specified ID exists.

This method supports checking the existence of both child and grand child modules.

public boolean hasModule($id)
$id string

Module ID. For grand child modules, use ID path relative to this module (e.g. admin/content).

return boolean

Whether the named module exists. Both loaded and unloaded modules are considered.

init() 公共 方法

Initializes the module.

This method is called after the module is created and initialized with property values given in configuration. The default implementation will initialize $controllerNamespace if it is not set.

If you override this method, please make sure you call the parent implementation.

public void init()
runAction() 公共 方法

Runs a controller action specified by a route.

This method parses the specified route and creates the corresponding child module(s), controller and action instances. It then calls yii\base\Controller::runAction() to run the action with the given parameters. If the route is empty, the method will use $defaultRoute.

public mixed runAction($route, $params = [])
$route string

The route that specifies the action.

$params array

The parameters to be passed to the action

return mixed

The result of the action.

throws yii\base\InvalidRouteException

if the requested route cannot be resolved into an action successfully.

setAliases() 公共 方法

Defines path aliases.

This method calls Yii::setAlias() to register the path aliases. This method is provided so that you can define path aliases when configuring a module.

public void setAliases($aliases)
$aliases array

List of path aliases to be defined. The array keys are alias names (must start with @) and the array values are the corresponding paths or aliases. For example,

[
    '@models' => '@app/models', // an existing alias
    '@backend' => __DIR__ . '/../backend',  // a directory
]
setBasePath() 公共 方法

Sets the root directory of the module.

This method can only be invoked at the beginning of the constructor.

public void setBasePath($path)
$path string

The root directory of the module. This can be either a directory name or a path alias.

throws yii\base\InvalidArgumentException

if the directory does not exist.

setInstance() 公共 静态 方法

Sets the currently requested instance of this module class.

public static void setInstance($instance)
$instance yii\base\Module|null

The currently requested instance of this module class. If it is null, the instance of the calling class will be removed, if any.

setLayoutPath() 公共 方法

Sets the directory that contains the layout files.

public void setLayoutPath($path)
$path string

The root directory or path alias of layout files.

throws yii\base\InvalidArgumentException

if the directory is invalid

setModule() 公共 方法

Adds a sub-module to this module.

public void setModule($id, $module)
$id string

Module ID.

$module yii\base\Module|array|null

The sub-module to be added to this module. This can be one of the following:

  • a yii\base\Module object
  • a configuration array: when getModule() is called initially, the array will be used to instantiate the sub-module
  • null: the named sub-module will be removed from this module
setModules() 公共 方法

Registers sub-modules in the current module.

Each sub-module should be specified as a name-value pair, where name refers to the ID of the module and value the module or a configuration array that can be used to create the module. In the latter case, Yii::createObject() will be used to create the module.

If a new sub-module has the same ID as an existing one, the existing one will be overwritten silently.

The following is an example for registering two sub-modules:

[
    'comment' => [
        'class' => 'app\modules\comment\CommentModule',
        'db' => 'db',
    ],
    'booking' => ['class' => 'app\modules\booking\BookingModule'],
]
public void setModules($modules)
$modules array

Modules (id => module configuration or instances).

setVersion() 公共 方法 (自版本 2.0.11 可用)

Sets current module version.

public void setVersion($version)
$version string|callable

The version of this module. Version can be specified as a PHP callback, which can accept module instance as an argument and should return the actual version. For example:

function (Module $module) {
    //return string
}
setViewPath() 公共 方法

Sets the directory that contains the view files.

public void setViewPath($path)
$path string

The root directory of view files.

throws yii\base\InvalidArgumentException

if the directory is invalid.

事件详情

EVENT_AFTER_ACTION event of type yii\base\ActionEvent

An event raised after executing a controller action.

EVENT_BEFORE_ACTION event of type yii\base\ActionEvent

An event raised before executing a controller action. You may set yii\base\ActionEvent::$isValid to be false to cancel the action execution.