# Module

> Module is the base class for module and application classes.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [base](/docs/api/yii/base/index.md) › Module

| class | `yii\base\Module` |
| --- | --- |
| Extends | [`yii\di\ServiceLocator`](/docs/api/yii/di/servicelocator/index.md) » [`Component`](/docs/api/yii/base/component/index.md) » [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/base/Module.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/base/Module.php#L55) |

2 subclasses:

- [`Application`](/docs/api/yii/base/application/index.md)
- [`yii\debug\Module`](/docs/extensions/debug/reference/module/index.md)

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](/docs/api/yii/base/module/index.md#property-modules).

[Components](/docs/api/yii/di/servicelocator/index.md#property-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](/docs/guide/structure-modules/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| [`$actionMap`](#property-actionMap) | `array<string, class-string\|array<string, mixed>>` | Mapping from action IDs to standalone [`Action`](/docs/api/yii/base/action/index.md) class configurations. Default: `[]`. Available since 22.0. | `Module` |
| [`$actionNamespace`](#property-actionNamespace) | `string\|null` | The namespace where standalone [`Action`](/docs/api/yii/base/action/index.md) subclasses are located for convention-based discovery, parallel to [`$controllerNamespace`](/docs/api/yii/base/module/index.md#property-controllerNamespace) for controllers. Default: `null`. Available since 22.0. | `Module` |
| [`$aliases`](#property-aliases) write-only | `array` | List of path aliases to be defined. | `Module` |
| <span id="property-basePath"></span>`$basePath` | `string` | The root directory of the module. | `Module` |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="property-components"></span>[`$components`](/docs/api/yii/di/servicelocator/index.md#property-components) | `array` | The list of the component definitions or the loaded component instances (ID => definition or instance). | [`yii\di\ServiceLocator`](/docs/api/yii/di/servicelocator/index.md) |
| [`$controllerMap`](#property-controllerMap) | `array` | Mapping from controller ID to controller configurations. Default: `[]`. | `Module` |
| [`$controllerNamespace`](#property-controllerNamespace) | `string\|null` | The namespace that controller classes are in. Default: `null`. | `Module` |
| <span id="property-controllerPath"></span>`$controllerPath` | `string` | The directory that contains the controller classes. | `Module` |
| [`$defaultRoute`](#property-defaultRoute) | `string` | The default route of this module. Default: `'default'`. | `Module` |
| <span id="property-id"></span>`$id` | `string` | An ID that uniquely identifies this module among other modules which have the same [parent](/docs/api/yii/base/module/index.md#property-module). Default: `null`. | `Module` |
| [`$layout`](#property-layout) | `string\|bool\|null` | The layout that should be applied for views within this module. Default: `null`. | `Module` |
| [`$layoutPath`](#property-layoutPath) | `string` | The root directory of layout files. | `Module` |
| <span id="property-module"></span>`$module` | [`Module`](/docs/api/yii/base/module/index.md)`\|null` | The parent module of this module. `null` if this module does not have a parent. Default: `null`. | `Module` |
| <span id="property-modules"></span>`$modules` | `array` | The modules (indexed by their IDs). | `Module` |
| <span id="property-params"></span>`$params` | `array` | Custom module parameters (name => value). Default: `[]`. | `Module` |
| <span id="property-uniqueId"></span>`$uniqueId` read-only | `string` | The unique ID of the module. | `Module` |
| [`$version`](#property-version) | `string\|callable\|null` | The version of this module. | `Module` |
| [`$viewPath`](#property-viewPath) | `string` | The root directory of view files. | `Module` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Constructor. | `Module` |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/di/servicelocator/index.md#method-__get) | Getter magic method. | [`yii\di\ServiceLocator`](/docs/api/yii/di/servicelocator/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/di/servicelocator/index.md#method-__isset) | Checks if a property value is null. | [`yii\di\ServiceLocator`](/docs/api/yii/di/servicelocator/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`afterAction()`](#method-afterAction) | This method is invoked right after an action within this module is executed. | `Module` |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`beforeAction()`](#method-beforeAction) | This method is invoked right before an action within this module is executed. | `Module` |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-clear"></span>[`clear()`](/docs/api/yii/di/servicelocator/index.md#method-clear) | Removes the component from the locator. | [`yii\di\ServiceLocator`](/docs/api/yii/di/servicelocator/index.md) |
| [`createController()`](#method-createController) | Creates a controller instance based on the given route. | `Module` |
| [`createControllerByID()`](#method-createControllerByID) | Creates a controller based on the given controller ID. | `Module` |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`get()`](#method-get) | Returns the component instance with the specified ID. | `Module` |
| [`getBasePath()`](#method-getBasePath) | Returns the root directory of the module. | `Module` |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getComponents"></span>[`getComponents()`](/docs/api/yii/di/servicelocator/index.md#method-getComponents) | Returns the list of the component definitions or the loaded component instances. | [`yii\di\ServiceLocator`](/docs/api/yii/di/servicelocator/index.md) |
| [`getControllerPath()`](#method-getControllerPath) | Returns the directory that contains the controller classes according to [`$controllerNamespace`](/docs/api/yii/base/module/index.md#property-controllerNamespace). | `Module` |
| [`getInstance()`](#method-getInstance) static | Returns the currently requested instance of this module class. | `Module` |
| [`getLayoutPath()`](#method-getLayoutPath) | Returns the directory that contains layout view files for this module. | `Module` |
| [`getModule()`](#method-getModule) | Retrieves the child module of the specified ID. | `Module` |
| [`getModules()`](#method-getModules) | Returns the sub-modules in this module. | `Module` |
| [`getUniqueId()`](#method-getUniqueId) | Returns an ID that uniquely identifies this module among all modules within the current application. | `Module` |
| [`getVersion()`](#method-getVersion) | Returns current module version. | `Module` |
| [`getViewPath()`](#method-getViewPath) | Returns the directory that contains the view files for this module. | `Module` |
| [`has()`](#method-has) | Returns a value indicating whether the locator has the specified component definition or has instantiated the component. | `Module` |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`hasModule()`](#method-hasModule) | Checks whether the child module of the specified ID exists. | `Module` |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`init()`](#method-init) | Initializes the module. | `Module` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`runAction()`](#method-runAction) | Runs a controller action specified by a route. | `Module` |
| <span id="method-set"></span>[`set()`](/docs/api/yii/di/servicelocator/index.md#method-set) | Registers a component definition with this locator. | [`yii\di\ServiceLocator`](/docs/api/yii/di/servicelocator/index.md) |
| [`setAliases()`](#method-setAliases) | Defines path aliases. | `Module` |
| [`setBasePath()`](#method-setBasePath) | Sets the root directory of the module. | `Module` |
| <span id="method-setComponents"></span>[`setComponents()`](/docs/api/yii/di/servicelocator/index.md#method-setComponents) | Registers a set of component definitions in this locator. | [`yii\di\ServiceLocator`](/docs/api/yii/di/servicelocator/index.md) |
| [`setControllerPath()`](#method-setControllerPath) | Sets the directory that contains the controller classes. | `Module` |
| [`setInstance()`](#method-setInstance) static | Sets the currently requested instance of this module class. | `Module` |
| [`setLayoutPath()`](#method-setLayoutPath) | Sets the directory that contains the layout files. | `Module` |
| [`setModule()`](#method-setModule) | Adds a sub-module to this module. | `Module` |
| [`setModules()`](#method-setModules) | Registers sub-modules in the current module. | `Module` |
| [`setVersion()`](#method-setVersion) | Sets current module version. | `Module` |
| [`setViewPath()`](#method-setViewPath) | Sets the directory that contains the view files. | `Module` |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`createStandaloneAction()`](#method-createStandaloneAction) protected | Resolves a route to a standalone [`Action`](/docs/api/yii/base/action/index.md) class through convention-based discovery under [`$actionNamespace`](/docs/api/yii/base/module/index.md#property-actionNamespace). | `Module` |
| [`defaultVersion()`](#method-defaultVersion) protected | Returns default module version. | `Module` |
| [`runMappedAction()`](#method-runMappedAction) protected | Runs a standalone action registered in [`$actionMap`](/docs/api/yii/base/module/index.md#property-actionMap). | `Module` |
| [`runStandaloneAction()`](#method-runStandaloneAction) protected | Runs a standalone [`Action`](/docs/api/yii/base/action/index.md) instance through the standard module/action event lifecycle. | `Module` |

## Events

| Event | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="event-EVENT_BEFORE_ACTION"></span>`EVENT_BEFORE_ACTION` | [`ActionEvent`](/docs/api/yii/base/actionevent/index.md) | An event raised before executing a controller action. Event name: `'beforeAction'`. | `Module` |
| <span id="event-EVENT_AFTER_ACTION"></span>`EVENT_AFTER_ACTION` | [`ActionEvent`](/docs/api/yii/base/actionevent/index.md) | An event raised after executing a controller action. Event name: `'afterAction'`. | `Module` |

## Property details

### $actionMap <span id="property-actionMap"></span>

```php
public array<string, class-string|array<string, mixed>> $actionMap = []
```

Mapping from action IDs to standalone [`Action`](/docs/api/yii/base/action/index.md) class configurations.

Mapped actions are dispatched by [`runAction()`](/docs/api/yii/base/module/index.md#method-runAction) without requiring a hosting controller, in
parallel to [`$controllerMap`](/docs/api/yii/base/module/index.md#property-controllerMap).

Each entry maps an ID (the first route segment) to either a fully qualified class name or an array configuration
accepted by [`Yii::createObject()`](/docs/api/yii/baseyii/index.md#method-createObject). The class must extend [`Action`](/docs/api/yii/base/action/index.md).

Usage example:

```php
[
    'webhooks-stripe' => app\actions\StripeWebhookAction::class,
    'health'          => ['class' => app\actions\HealthCheckAction::class],
]
```

Action IDs in this map take precedence over IDs in [`$controllerMap`](/docs/api/yii/base/module/index.md#property-controllerMap) and over module/controller resolution.

Routes whose first segment is not present here fall through to the legacy controller pipeline unchanged.

Resolution scope: this map is consulted only by the module whose [`runAction()`](/docs/api/yii/base/module/index.md#method-runAction) handles the route (typically
the application). Unlike [`$controllerMap`](/docs/api/yii/base/module/index.md#property-controllerMap), it is NOT resolved recursively across sub-modules during
nested-route dispatch. To expose a standalone action under a sub-module, rely on [`$actionNamespace`](/docs/api/yii/base/module/index.md#property-actionNamespace) (which
IS resolved recursively), or register the action in the dispatching module's [`$actionMap`](/docs/api/yii/base/module/index.md#property-actionMap).

**Since**

22.0

### $actionNamespace <span id="property-actionNamespace"></span>

```php
public string|null $actionNamespace = null
```

The namespace where standalone [`Action`](/docs/api/yii/base/action/index.md) subclasses are located for convention-based discovery, parallel to
[`$controllerNamespace`](/docs/api/yii/base/module/index.md#property-controllerNamespace) for controllers.

If not set, defaults to the value of [`$controllerNamespace`](/docs/api/yii/base/module/index.md#property-controllerNamespace) during [`init()`](/docs/api/yii/base/module/index.md#method-init), so standalone actions can
live next to controllers under the same root. Set this property to a different namespace to organize standalone
actions in their own folder (for example, vertical slices under `app\UseCase`):

Usage example:

```php
'actionNamespace' => 'app\\UseCase',
```

With the example above, route `posts/index` resolves to `app\UseCase\posts\IndexAction`, route
`posts/view-summary` resolves to `app\UseCase\posts\ViewSummaryAction`, and so on. The same hyphen, slash, and
CamelCase rules apply as for controllers (see [`createControllerByID()`](/docs/api/yii/base/module/index.md#method-createControllerByID)).

Resolution order in [`runAction()`](/docs/api/yii/base/module/index.md#method-runAction): explicit [`$controllerMap`](/docs/api/yii/base/module/index.md#property-controllerMap)/[`$actionMap`](/docs/api/yii/base/module/index.md#property-actionMap) entries are checked
first, then sub-modules, then controllers by namespace, and finally standalone actions by namespace. When a
controller method AND a standalone Action class both match the same route, [`runAction()`](/docs/api/yii/base/module/index.md#method-runAction) throws
[`InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md) so the developer disambiguates instead of silently shadowing one or the other.

**Since**

22.0

### $aliases <span id="property-aliases"></span>

```php
public array $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
]
```

**Access**

Write-only (virtual property).

### $controllerMap <span id="property-controllerMap"></span>

```php
public array $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',
  ],
]
```

### $controllerNamespace <span id="property-controllerNamespace"></span>

```php
public string|null $controllerNamespace = null
```

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](/docs/guide/concept-autoloading/index.md) to learn more about
defining namespaces and how classes are loaded.

### $defaultRoute <span id="property-defaultRoute"></span>

```php
public string $defaultRoute = 'default'
```

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`](/docs/api/yii/base/controller/index.md#property-defaultAction).

### $layout <span id="property-layout"></span>

```php
public string|bool|null $layout = null
```

The layout that should be applied for views within this module.

This refers to a view name
relative to [`$layoutPath`](/docs/api/yii/base/module/index.md#property-layoutPath). If this is not set, it means the layout value of the [parent module](/docs/api/yii/base/module/index.md#property-module)
will be taken. If this is `false`, layout will be disabled within this module.

### $layoutPath <span id="property-layoutPath"></span>

```php
public string $layoutPath
```

The root directory of layout files.

Defaults to "[`$viewPath`](/docs/api/yii/base/module/index.md#property-viewPath)/layouts".

### $version <span id="property-version"></span>

```php
public string|callable|null $version
```

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
}
```

### $viewPath <span id="property-viewPath"></span>

```php
public string $viewPath
```

The root directory of view files.

Defaults to "[`$basePath`](/docs/api/yii/base/module/index.md#property-basePath)/views".

## Method details

### __construct() <span id="method-__construct"></span>

```php
public function __construct(string $id, yii\base\Module|null $parent = null, array<string, mixed> $config = [])
```

Constructor.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$id` | `string` | The ID of this module. |
| `$parent` | [`Module`](/docs/api/yii/base/module/index.md)`\|null` | The parent module (if any). |
| `$config` | `array<string, mixed>` | Name-value pairs that will be used to initialize the object properties. |

### afterAction() <span id="method-afterAction"></span>

```php
public function afterAction(yii\base\Action<yii\base\Controller<static>> $action, mixed $result): mixed
```

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

The method will trigger the [`EVENT_AFTER_ACTION`](/docs/api/yii/base/module/index.md#event-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;
}
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$action` | [`Action`](/docs/api/yii/base/action/index.md)`<`[`Controller`](/docs/api/yii/base/controller/index.md)`<static>>` | The action just executed. |
| `$result` | `mixed` | The action return result. |

**Returns**

`mixed`: The processed action result.

### beforeAction() <span id="method-beforeAction"></span>

```php
public function beforeAction(yii\base\Action<yii\base\Controller<static>> $action): bool
```

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

The method will trigger the [`EVENT_BEFORE_ACTION`](/docs/api/yii/base/module/index.md#event-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
}
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$action` | [`Action`](/docs/api/yii/base/action/index.md)`<`[`Controller`](/docs/api/yii/base/controller/index.md)`<static>>` | The action to be executed. |

**Returns**

`bool`: Whether the action should continue to be executed.

### createController() <span id="method-createController"></span>

```php
public function createController(string $route): array{yii\base\Controller<static>, string}|false
```

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`](/docs/api/yii/base/module/index.md#property-defaultRoute);
2. If the first segment of the route is found in [`$controllerMap`](/docs/api/yii/base/module/index.md#property-controllerMap), create a controller
   based on the corresponding configuration found in [`$controllerMap`](/docs/api/yii/base/module/index.md#property-controllerMap);
3. If the first segment of the route is a valid module ID as declared in [`$modules`](/docs/api/yii/base/module/index.md#property-modules),
   call the module's `createController()` with the rest part of the route;
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](/docs/api/yii/base/module/index.md#property-controllerNamespace).

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.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$route` | `string` | The route consisting of module, controller and action IDs. |

**Returns**

`array{`[`Controller`](/docs/api/yii/base/controller/index.md)`<static>, string}|false`: If the controller is created successfully, it will be returned together with the requested action ID. Otherwise `false` will be returned.

**Throws**

- [`InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the controller class and its file do not match.

### createControllerByID() <span id="method-createControllerByID"></span>

```php
public function createControllerByID(string $id): yii\base\Controller<static>|null
```

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`](/docs/api/yii/base/module/index.md#property-controllerNamespace).

Note that this method does not check [`$modules`](/docs/api/yii/base/module/index.md#property-modules) or [`$controllerMap`](/docs/api/yii/base/module/index.md#property-controllerMap).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$id` | `string` | The controller ID. |

**Returns**

[`Controller`](/docs/api/yii/base/controller/index.md)`<static>|null`: The newly created controller instance, or `null` if the controller ID is invalid.

**Throws**

- [`InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the controller class and its file name do not match. This exception is only thrown when in debug mode.

### get() <span id="method-get"></span>

```php
public function get(mixed $id, mixed $throwException = true): ($throwException is true ? object : object|null)
```

Returns the component instance with the specified 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.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$id` | `mixed` | Component ID (e.g. `db`). |
| `$throwException` | `mixed` | Whether to throw an exception if `$id` is not registered with the locator before. |

**Returns**

`($throwException is true ? object : object|null)`: The component of the specified ID. If `$throwException` is false and `$id` is not registered before, null will be returned.

**Throws**

- [`InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if `$id` refers to a nonexistent component ID

### getBasePath() <span id="method-getBasePath"></span>

```php
public function getBasePath(): string
```

Returns the root directory of the module.

It defaults to the directory containing the module class file.

**Returns**

`string`: The root directory of the module.

### getControllerPath() <span id="method-getControllerPath"></span>

```php
public function getControllerPath(): string
```

Returns the directory that contains the controller classes according to [`$controllerNamespace`](/docs/api/yii/base/module/index.md#property-controllerNamespace).

Note that in order for this method to return a value, you must define
an alias for the root namespace of [`$controllerNamespace`](/docs/api/yii/base/module/index.md#property-controllerNamespace).

**Returns**

`string`: The directory that contains the controller classes.

**Throws**

- [`InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): if there is no alias defined for the root namespace of [`$controllerNamespace`](/docs/api/yii/base/module/index.md#property-controllerNamespace).

### getInstance() <span id="method-getInstance"></span>

```php
public static function getInstance(): static|null
```

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.

**Returns**

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

### getLayoutPath() <span id="method-getLayoutPath"></span>

```php
public function getLayoutPath(): string
```

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

**Returns**

`string`: The root directory of layout files. Defaults to "[`$viewPath`](/docs/api/yii/base/module/index.md#property-viewPath)/layouts".

### getModule() <span id="method-getModule"></span>

```php
public function getModule(string $id, bool $load = true): yii\base\Module|null
```

Retrieves the child module of the specified ID.

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

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$id` | `string` | Module ID (case-sensitive). To retrieve grand child modules, use ID path relative to this module (e.g. `admin/content`). |
| `$load` | `bool` | Whether to load the module if it is not yet loaded. |

**Returns**

[`Module`](/docs/api/yii/base/module/index.md)`|null`: The module instance, `null` if the module does not exist.

**See also**

- [`hasModule()`](/docs/api/yii/base/module/index.md#method-hasModule)

### getModules() <span id="method-getModules"></span>

```php
public function getModules(bool $loadedOnly = false): array
```

Returns the sub-modules in this module.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$loadedOnly` | `bool` | 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. |

**Returns**

`array`: The modules (indexed by their IDs).

### getUniqueId() <span id="method-getUniqueId"></span>

```php
public function getUniqueId(): string
```

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.

**Returns**

`string`: The unique ID of the module.

### getVersion() <span id="method-getVersion"></span>

```php
public function getVersion(): string
```

Returns current module version.

If version is not explicitly set, [`defaultVersion()`](/docs/api/yii/base/module/index.md#method-defaultVersion) method will be used to determine its value.

**Returns**

`string`: The version of this module.

**Since**

2.0.11

### getViewPath() <span id="method-getViewPath"></span>

```php
public function getViewPath(): string
```

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

**Returns**

`string`: The root directory of view files. Defaults to "[`$basePath`](/docs/api/yii/base/module/index.md#property-basePath)/views".

### has() <span id="method-has"></span>

```php
public function has(mixed $id, mixed $checkInstance = false): bool
```

Returns a value indicating whether the locator has the specified component definition or has instantiated the component.

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.

This method may return different results depending on the value of `$checkInstance`.

- If `$checkInstance` is false (default), the method will return a value indicating whether the locator has the specified
  component definition.
- If `$checkInstance` is true, the method will return a value indicating whether the locator has
  instantiated the specified component.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$id` | `mixed` | Component ID (e.g. `db`). |
| `$checkInstance` | `mixed` | Whether the method should check if the component is shared and instantiated. |

**Returns**

`bool`: Whether the locator has the specified component definition or has instantiated the component.

### hasModule() <span id="method-hasModule"></span>

```php
public function hasModule(string $id): bool
```

Checks whether the child module of the specified ID exists.

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

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$id` | `string` | Module ID. For grand child modules, use ID path relative to this module (e.g. `admin/content`). |

**Returns**

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

### init() <span id="method-init"></span>

```php
public function init(): mixed
```

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`](/docs/api/yii/base/module/index.md#property-controllerNamespace) if it is not set.

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

**Returns**

`mixed`

### runAction() <span id="method-runAction"></span>

```php
public function runAction(string $route, array $params = []): mixed
```

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 [`Controller::runAction()`](/docs/api/yii/base/controller/index.md#method-runAction) to run the action with the given parameters.

If the route is empty, the method will use [`$defaultRoute`](/docs/api/yii/base/module/index.md#property-defaultRoute).

Since version 22.0, when the first segment of the route matches a key in [`$actionMap`](/docs/api/yii/base/module/index.md#property-actionMap), the action is dispatched
as a standalone action via [`runMappedAction()`](/docs/api/yii/base/module/index.md#method-runMappedAction), without resolving a controller. When no controller resolves the
route either, the method falls back to convention-based standalone action lookup under [`$actionNamespace`](/docs/api/yii/base/module/index.md#property-actionNamespace) via
[`createStandaloneAction()`](/docs/api/yii/base/module/index.md#method-createStandaloneAction). When a controller method AND a standalone Action class both match the same route,
the method throws [`InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md) so the developer disambiguates instead of silently shadowing one or
the other.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$route` | `string` | The route that specifies the action. |
| `$params` | `array` | The parameters to be passed to the action. |

**Returns**

`mixed`: The result of the action.

**Throws**

- [`InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if a controller method and a standalone Action class both match the same route.
- [`InvalidRouteException`](/docs/api/yii/base/invalidrouteexception/index.md): if the requested route cannot be resolved into an action successfully.

### setAliases() <span id="method-setAliases"></span>

```php
public function setAliases(array $aliases): mixed
```

Defines path aliases.

This method calls [`Yii::setAlias()`](/docs/api/yii/baseyii/index.md#method-setAlias) to register the path aliases.
This method is provided so that you can define path aliases when configuring a module.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$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 ] |

**Returns**

`mixed`

### setBasePath() <span id="method-setBasePath"></span>

```php
public function setBasePath(string $path): mixed
```

Sets the root directory of the module.

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

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$path` | `string` | The root directory of the module. This can be either a directory name or a [path alias](/docs/guide/concept-aliases/index.md). |

**Returns**

`mixed`

**Throws**

- [`InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): if the directory does not exist.

### setControllerPath() <span id="method-setControllerPath"></span>

```php
public function setControllerPath(string $path): mixed
```

Sets the directory that contains the controller classes.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$path` | `string` | The root directory that contains the controller classes. |

**Returns**

`mixed`

**Throws**

- [`InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): if the directory is invalid.

**Since**

2.0.44

### setInstance() <span id="method-setInstance"></span>

```php
public static function setInstance(yii\base\Module|null $instance): mixed
```

Sets the currently requested instance of this module class.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$instance` | [`Module`](/docs/api/yii/base/module/index.md)`\|null` | The currently requested instance of this module class. If it is `null`, the instance of the calling class will be removed, if any. |

**Returns**

`mixed`

### setLayoutPath() <span id="method-setLayoutPath"></span>

```php
public function setLayoutPath(string $path): mixed
```

Sets the directory that contains the layout files.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$path` | `string` | The root directory or [path alias](/docs/guide/concept-aliases/index.md) of layout files. |

**Returns**

`mixed`

**Throws**

- [`InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): if the directory is invalid

### setModule() <span id="method-setModule"></span>

```php
public function setModule(string $id, yii\base\Module|array|null $module): mixed
```

Adds a sub-module to this module.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$id` | `string` | Module ID. |
| `$module` | [`Module`](/docs/api/yii/base/module/index.md)`\|array\|null` | The sub-module to be added to this module. This can be one of the following: - a [`Module`](/docs/api/yii/base/module/index.md) object - a configuration array: when [`getModule()`](/docs/api/yii/base/module/index.md#method-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 |

**Returns**

`mixed`

### setModules() <span id="method-setModules"></span>

```php
public function setModules(array $modules): mixed
```

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()`](/docs/api/yii/baseyii/index.md#method-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'],
]
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$modules` | `array` | Modules (id => module configuration or instances). |

**Returns**

`mixed`

### setVersion() <span id="method-setVersion"></span>

```php
public function setVersion(string|callable|null $version): mixed
```

Sets current module version.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$version` | `string\|callable\|null` | 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 } |

**Returns**

`mixed`

**Since**

2.0.11

### setViewPath() <span id="method-setViewPath"></span>

```php
public function setViewPath(string $path): mixed
```

Sets the directory that contains the view files.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$path` | `string` | The root directory of view files. |

**Returns**

`mixed`

**Throws**

- [`InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): if the directory is invalid.

### createStandaloneAction() <span id="method-createStandaloneAction"></span>

```php
protected function createStandaloneAction(string $route): yii\base\Action|null
```

Resolves a route to a standalone [`Action`](/docs/api/yii/base/action/index.md) class through convention-based discovery under
[`$actionNamespace`](/docs/api/yii/base/module/index.md#property-actionNamespace).

Recurses through registered sub-modules using their first-segment ID, matching the pattern of
[`createController()`](/docs/api/yii/base/module/index.md#method-createController). When the route does not match a sub-module, the last segment is converted to a
CamelCased class name with an `Action` suffix, the preceding segments form a sub-namespace prefix, and the result
is looked up under the owning module's [`$actionNamespace`](/docs/api/yii/base/module/index.md#property-actionNamespace).

The resolved class must extend [`Action`](/docs/api/yii/base/action/index.md) and must NOT extend [`Controller`](/docs/api/yii/base/controller/index.md); otherwise this method returns
`null` and the caller falls through to the controller pipeline (or to `InvalidRouteException`).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$route` | `string` | The route relative to this module. |

**Returns**

[`Action`](/docs/api/yii/base/action/index.md)`|null`: A resolved standalone action ready for dispatch, or `null` when no class matches.

**Since**

22.0

### defaultVersion() <span id="method-defaultVersion"></span>

```php
protected function defaultVersion(): string
```

Returns default module version.

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

**Returns**

`string`: The version of this module.

**Since**

2.0.11

### runMappedAction() <span id="method-runMappedAction"></span>

```php
protected function runMappedAction(string $id, array $params): mixed
```

Runs a standalone action registered in [`$actionMap`](/docs/api/yii/base/module/index.md#property-actionMap).

The action is instantiated via [`Yii::createObject()`](/docs/api/yii/baseyii/index.md#method-createObject) with no positional constructor arguments, allowing the
DI container to autowire typed dependencies declared on the action's constructor. The dispatcher assigns
[`Action::$id`](/docs/api/yii/base/action/index.md#property-id) after construction and then delegates lifecycle to [`runStandaloneAction()`](/docs/api/yii/base/module/index.md#method-runStandaloneAction).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$id` | `string` | The action ID present in [`$actionMap`](/docs/api/yii/base/module/index.md#property-actionMap). |
| `$params` | `array` | The parameters to be matched against the action's `run()` signature. |

**Returns**

`mixed`: The action result, or `null` if execution was canceled by a before-action listener.

**Since**

22.0

### runStandaloneAction() <span id="method-runStandaloneAction"></span>

```php
protected function runStandaloneAction(yii\base\Action $action, array $params): mixed
```

Runs a standalone [`Action`](/docs/api/yii/base/action/index.md) instance through the standard module/action event lifecycle.

Module ancestors fire [`EVENT_BEFORE_ACTION`](/docs/api/yii/base/module/index.md#event-EVENT_BEFORE_ACTION) outer-to-inner before execution and [`EVENT_AFTER_ACTION`](/docs/api/yii/base/module/index.md#event-EVENT_AFTER_ACTION)
inner-to-outer afterwards, mirroring [`Controller::runAction()`](/docs/api/yii/base/controller/index.md#method-runAction). The action itself also fires
[`Controller::EVENT_BEFORE_ACTION`](/docs/api/yii/base/controller/index.md#event-EVENT_BEFORE_ACTION) and [`Controller::EVENT_AFTER_ACTION`](/docs/api/yii/base/controller/index.md#event-EVENT_AFTER_ACTION) so behaviors attached via the action's
[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) (for example [`AccessControl`](/docs/api/yii/filters/accesscontrol/index.md) or [`VerbFilter`](/docs/api/yii/filters/verbfilter/index.md)) participate in the
lifecycle.

Setting [`ActionEvent::$isValid`](/docs/api/yii/base/actionevent/index.md#property-isValid) to `false` in any before-action listener cancels the action and returns `null`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$action` | [`Action`](/docs/api/yii/base/action/index.md) | The action to run. |
| `$params` | `array` | The parameters to be matched against the action's `run()` signature. |

**Returns**

`mixed`: The action result, or `null` if execution was canceled by a before-action listener.

**Since**

22.0