A module is instantiated when one of its controllers is accessed by end users. You may access the instance of a module
using the approaches shown in the following example:
```php
// get the module whose ID is "forum"
$module=\Yii::$app->getModule('forum');
## Using Modules
// get the module to which the currently requested controller belongs
$module=\Yii::$app->controller->module;
```
To use a module, first place the module directory under `modules` of the [application base directory](/doc/guide/basics.application#application-base-directory). Then declare the module ID in the [modules|CWebApplication::modules] property of the application. For example, in order to use the above `forum` module, we can use the following [application configuration](/doc/guide/basics.application#application-configuration):
The first approach is only useful in application code which has knowledge about the module ID, while the second approach
is best used by the code within the module.
~~~
[php]
return array(
......
'modules'=>array('forum',...),
......
);
~~~
Once getting hold of a module instance, you can access parameters or components registered with the module. For example,
A module can also be configured with initial property values. The usage is very similar to configuring [application components](/doc/guide/basics.application#application-component). For example, the `forum` module may have a property named `postPerPage` in its module class which can be configured in the [application configuration](/doc/guide/basics.application#application-configuration) as follows:
```php
$maxPostCount=$module->params['maxPostCount'];
```
~~~
[php]
return array(
......
'modules'=>array(
'forum'=>array(
'postPerPage'=>20,
),
),
......
);
~~~
The module instance may be accessed via the [module|CController::module] property of the currently active controller. Through the module instance, we can then access information that are shared at the module level. For example, in order to access the above `postPerPage` information, we can use the following expression:
// or the following if $this refers to the controller instance
// $postPerPage=$this->module->postPerPage;
~~~
Some modules may need to be run for every request. The [[yii\debug\Module|debug]] module is such an example.
To do so, list the IDs of such modules in the [[yii\base\Application::bootstrap|bootstrap]] property of the application.
A controller action in a module can be accessed using the [route](/doc/guide/basics.controller#route)`moduleID/controllerID/actionID`. For example, assuming the above `forum` module has a controller named `PostController`, we can use the [route](/doc/guide/basics.controller#route)`forum/post/create` to refer to the `create` action in this controller. The corresponding URL for this route would be `http://www.example.com/index.php?r=forum/post/create`.
For example, the following application configuration makes sure the `debug` module is always load:
```php
[
'bootstrap'=>[
'debug',
],
'modules'=>[
'debug'=>'yii\debug\Module',
],
]
```
> Tip: If a controller is in a sub-directory of `controllers`, we can still use the above [route](/doc/guide/basics.controller#route) format. For example, assuming `PostController` is under `forum/controllers/admin`, we can refer to the `create` action using `forum/admin/post/create`.
## Nested Modules <a name="nested-modules"></a>
## Nested Modules
Modules can be nested in unlimited levels. That is, a module can contain another module which can contain yet
another module. We call the former *parent module* while the latter *child module*. Child modules must be declared
in the [[yii\bas\Module::modules|modules]] property of their parent modules. For example,
Modules can be nested in unlimited levels. That is, a module can contain another module which can contain yet another module. We call the former *parent module* while the latter *child module*. Child modules must be declared in the [modules|CWebModule::modules] property of their parent module, like we declare modules in the application configuration shown as above.
```php
namespaceapp\modules\forum;
To access a controller action in a child module, we should use the route `parentModuleID/childModuleID/controllerID/actionID`.
classModuleextends\yii\base\Module
{
publicfunctioninit()
{
parent::init();
$this->modules=[
'admin'=>[
// you should consider using a shorter namespace here!