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
파일 보기 프로젝트 열기: yiisoft/yii2 1 사용 예제들

공개 프로퍼티들

프로퍼티 타입 설명
$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