PHP Класс yii\base\Module

A module represents a sub-application which contains MVC elements by itself, such as models, views, controllers, etc. A module may consist of [[modules|sub-modules]]. [[components|Components]] may be registered with the module so that they are globally accessible within the module.
С версии: 2.0
Автор: Qiang Xue ([email protected])
Наследование: extends yii\di\ServiceLocator
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$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, php [ 'account' => 'app\controllers\UserController', 'article' => [ 'class' => 'app\controllers\PostController', 'pageTitle' => 'something new', ], ]
$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.
$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 [[Controller::defaultAction]].
$id an ID that uniquely identifies this module among other modules which have the same [[module|parent]].
$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 [[module|parent module]] will be taken. If this is false, layout will be disabled within this module.
$module the parent module of this module. null if this module does not have a parent.
$params custom module parameters (name => value).

Открытые методы

Метод Описание
__construct ( string $id, Module $parent = null, array $config = [] ) Constructor.
afterAction ( Action $action, mixed $result ) : mixed This method is invoked right after an action within this module is executed.
beforeAction ( Action $action ) : boolean This method is invoked right before an action within this module is executed.
createController ( string $route ) : array | boolean Creates a controller instance based on the given route.
createControllerByID ( string $id ) : Controller Creates a controller based on the given controller ID.
getBasePath ( ) : string Returns the root directory of the module.
getControllerPath ( ) : string Returns the directory that contains the controller classes according to [[controllerNamespace]].
getInstance ( ) : static | null Returns the currently requested instance of this module class.
getLayoutPath ( ) : string Returns the directory that contains layout view files for this module.
getModule ( string $id, boolean $load = true ) : Module | null Retrieves the child module of the specified ID.
getModules ( boolean $loadedOnly = false ) : array Returns the sub-modules in this module.
getUniqueId ( ) : string Returns an ID that uniquely identifies this module among all modules within the current application.
getVersion ( ) : string Returns current module version.
getViewPath ( ) : string Returns the directory that contains the view files for this module.
hasModule ( string $id ) : boolean Checks whether the child module of the specified ID exists.
init ( ) Initializes the module.
runAction ( string $route, array $params = [] ) : mixed Runs a controller action specified by a route.
setAliases ( array $aliases ) Defines path aliases.
setBasePath ( string $path ) Sets the root directory of the module.
setInstance ( Module | null $instance ) Sets the currently requested instance of this module class.
setLayoutPath ( string $path ) Sets the directory that contains the layout files.
setModule ( string $id, Module | array | null $module ) Adds a sub-module to this module.
setModules ( array $modules ) Registers sub-modules in the current module.
setVersion ( string | callable $version ) Sets current module version.
setViewPath ( string $path ) Sets the directory that contains the view files.

Защищенные методы

Метод Описание
defaultVersion ( ) : string Returns default module version.

Описание методов

__construct() публичный метод

Constructor.
public __construct ( string $id, Module $parent = null, array $config = [] )
$id string the ID of this module.
$parent Module the parent module (if any).
$config array name-value pairs that will be used to initialize the object properties.

afterAction() публичный метод

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: php public function afterAction($action, $result) { $result = parent::afterAction($action, $result); your custom code here return $result; }
public afterAction ( Action $action, mixed $result ) : mixed
$action Action the action just executed.
$result mixed the action return result.
Результат mixed the processed action result.

beforeAction() публичный метод

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: php public function beforeAction($action) { if (!parent::beforeAction($action)) { return false; } your custom code here return true; // or false to not run the action }
public beforeAction ( Action $action ) : boolean
$action Action the action to be executed.
Результат boolean whether the action should continue to be executed.

createController() публичный метод

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 [[controllerNamespace|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 createController ( string $route ) : array | boolean
$route string the route consisting of module, controller and action IDs.
Результат array | boolean If the controller is created successfully, it will be returned together with the requested action ID. Otherwise `false` will be returned.

createControllerByID() публичный метод

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 createControllerByID ( string $id ) : Controller
$id string the controller ID.
Результат Controller the newly created controller instance, or `null` if the controller ID is invalid.

defaultVersion() защищенный метод

Child class may override this method to provide more specific version detection.
С версии: 2.0.11
protected defaultVersion ( ) : string
Результат string the version of this module.

getBasePath() публичный метод

It defaults to the directory containing the module class file.
public getBasePath ( ) : string
Результат string the root directory of the module.

getControllerPath() публичный метод

Note that in order for this method to return a value, you must define an alias for the root namespace of [[controllerNamespace]].
public getControllerPath ( ) : string
Результат string the directory that contains the controller classes.

getInstance() публичный статический метод

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 getInstance ( ) : static | null
Результат 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 getLayoutPath ( ) : string
Результат string the root directory of layout files. Defaults to "[[viewPath]]/layouts".

getModule() публичный метод

This method supports retrieving both child modules and grand child modules.
См. также: hasModule()
public getModule ( string $id, boolean $load = true ) : Module | null
$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.
Результат Module | null the module instance, `null` if the module does not exist.

getModules() публичный метод

Returns the sub-modules in this module.
public getModules ( boolean $loadedOnly = false ) : array
$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.
Результат array the modules (indexed by their IDs).

getUniqueId() публичный метод

Note that if the module is an application, an empty string will be returned.
public getUniqueId ( ) : string
Результат string the unique ID of the module.

getVersion() публичный метод

If version is not explicitly set, Module::defaultVersion method will be used to determine its value.
С версии: 2.0.11
public getVersion ( ) : string
Результат string the version of this module.

getViewPath() публичный метод

Returns the directory that contains the view files for this module.
public getViewPath ( ) : string
Результат string the root directory of view files. Defaults to "[[basePath]]/views".

hasModule() публичный метод

This method supports checking the existence of both child and grand child modules.
public hasModule ( string $id ) : boolean
$id string module ID. For grand child modules, use ID path relative to this module (e.g. `admin/content`).
Результат boolean whether the named module exists. Both loaded and unloaded modules are considered.

init() публичный метод

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 init ( )

runAction() публичный метод

This method parses the specified route and creates the corresponding child module(s), controller and action instances. It then calls [[Controller::runAction()]] to run the action with the given parameters. If the route is empty, the method will use [[defaultRoute]].
public runAction ( string $route, array $params = [] ) : mixed
$route string the route that specifies the action.
$params array the parameters to be passed to the action
Результат mixed the result of the action.

setAliases() публичный метод

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 setAliases ( array $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, ```php [ '@models' => '@app/models', // an existing alias '@backend' => __DIR__ . '/../backend', // a directory ] ```

setBasePath() публичный метод

This method can only be invoked at the beginning of the constructor.
public setBasePath ( string $path )
$path string the root directory of the module. This can be either a directory name or a path alias.

setInstance() публичный статический метод

Sets the currently requested instance of this module class.
public static setInstance ( Module | null $instance )
$instance 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 setLayoutPath ( string $path )
$path string the root directory or path alias of layout files.

setModule() публичный метод

Adds a sub-module to this module.
public setModule ( string $id, Module | array | null $module )
$id string module ID.
$module Module | array | null the sub-module to be added to this module. This can be one of the following: - a [[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() публичный метод

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: php [ 'comment' => [ 'class' => 'app\modules\comment\CommentModule', 'db' => 'db', ], 'booking' => ['class' => 'app\modules\booking\BookingModule'], ]
public setModules ( array $modules )
$modules array modules (id => module configuration or instances).

setVersion() публичный метод

Sets current module version.
С версии: 2.0.11
public setVersion ( string | callable $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: ```php function (Module $module) { //return string } ```

setViewPath() публичный метод

Sets the directory that contains the view files.
public setViewPath ( string $path )
$path string the root directory of view files.

Описание свойств

$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, php [ 'account' => 'app\controllers\UserController', 'article' => [ 'class' => 'app\controllers\PostController', 'pageTitle' => 'something new', ], ]
public $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.
public $controllerNamespace

$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 [[Controller::defaultAction]].
public $defaultRoute

$id публичное свойство

an ID that uniquely identifies this module among other modules which have the same [[module|parent]].
public $id

$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 [[module|parent module]] will be taken. If this is false, layout will be disabled within this module.
public $layout

$module публичное свойство

the parent module of this module. null if this module does not have a parent.
public $module

$params публичное свойство

custom module parameters (name => value).
public $params