# Action

> Action is the base class for all controller action classes.

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

| class | `yii\base\Action` |
| --- | --- |
| Extends | [`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/Action.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/base/Action.php#L49) |

7 subclasses:

- [`InlineAction`](/docs/api/yii/base/inlineaction/index.md)
- [`yii\captcha\CaptchaAction`](/docs/api/yii/captcha/captchaaction/index.md)
- [`yii\rest\Action`](/docs/api/yii/rest/action/index.md)
- [`yii\rest\OptionsAction`](/docs/api/yii/rest/optionsaction/index.md)
- [`yii\web\Action`](/docs/api/yii/web/action/index.md)
- [`yii\web\ErrorAction`](/docs/api/yii/web/erroraction/index.md)
- [`yii\web\ViewAction`](/docs/api/yii/web/viewaction/index.md)

Action is the base class for all controller action classes.

Action provides a way to reuse action method code. An action method in an Action class can be used in multiple
controllers or in different projects.

Derived classes must implement a method named `run()`. This method will be invoked by the controller when the action
is requested.

The `run()` method can have parameters which will be filled up with user input values automatically according to
their names.

For example, if the `run()` method is declared as follows:

```
public function run($id, $type = 'book') { ... }
```

And the parameters provided for the action are: `['id' => 1]`.

Then the `run()` method will be invoked as `run(1)` automatically.

For more details and usage information on Action, see the [guide article on actions](/docs/guide/structure-controllers/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <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-controller"></span>`$controller` | `yii\base\T\|null` | The controller that owns this action, or `null` when the action runs standalone. via [`Module::$actionMap`](/docs/api/yii/base/module/index.md#property-actionMap) Default: `null`. | `Action` |
| <span id="property-id"></span>`$id` | `string\|null` | ID of the action; `null` between construction and dispatcher post-assignment for standalone actions resolved through [`Module::$actionMap`](/docs/api/yii/base/module/index.md#property-actionMap) or [`Module::$actionNamespace`](/docs/api/yii/base/module/index.md#property-actionNamespace). Default: `null`. | `Action` |
| <span id="property-module"></span>`$module` | [`Module`](/docs/api/yii/base/module/index.md)`\|null` | The owning module, or `null` if neither a module nor a controller is attached. Available since 22.0. | `Action` |
| <span id="property-uniqueId"></span>`$uniqueId` read-only | `string` | The unique ID of this action among the whole application. | `Action` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Constructor. | `Action` |
| <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/base/component/index.md#method-__get) | Returns the value of a component property. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`Component`](/docs/api/yii/base/component/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) |
| <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) |
| <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-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) |
| <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) |
| [`getModule()`](#method-getModule) | Returns the module that owns this action when invoked standalone. | `Action` |
| [`getUniqueId()`](#method-getUniqueId) | Returns the unique ID of this action among the whole application. | `Action` |
| <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) |
| <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) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <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) |
| [`runWithParams()`](#method-runWithParams) | Runs this action with the specified parameters. | `Action` |
| [`setModule()`](#method-setModule) | Sets the module that owns this action when invoked standalone. | `Action` |
| <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) |
| [`afterRun()`](#method-afterRun) protected | This method is called right after `run()` is executed. | `Action` |
| [`beforeRun()`](#method-beforeRun) protected | This method is called right before `run()` is executed. | `Action` |
| [`resolveStandaloneParams()`](#method-resolveStandaloneParams) protected | Resolves typed parameters of `run()` when the action is invoked without a hosting controller. | `Action` |

## Method details

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

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

Constructor.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$id` | `string\|null` | The ID of this action, or `null` when the action is instantiated through the DI container and the ID is assigned afterwards by the dispatcher. |
| `$controller` | `yii\base\T\|null` | The controller that owns this action, or `null` for standalone handlers. |
| `$config` | `array<string, mixed>` | Name-value pairs that will be used to initialize the object properties. |

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

```php
public function getModule(): yii\base\Module|null
```

Returns the module that owns this action when invoked standalone.

Falls back to the controller's module when a controller hosts the action.

**Returns**

[`Module`](/docs/api/yii/base/module/index.md)`|null`: The owning module, or `null` if neither a module nor a controller is attached.

**Since**

22.0

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

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

Returns the unique ID of this action among the whole application.

When the action is hosted by a controller, the ID is composed of the controller's unique ID and the action ID.

For standalone handlers (no controller), the ID falls back to the owning module's unique ID, or to the action ID
alone when no module is attached.

**Returns**

`string`: The unique ID of this action among the whole application.

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

```php
public function runWithParams(array $params): mixed
```

Runs this action with the specified parameters.

When a controller hosts the action, parameter binding is delegated to [`Controller::bindActionParams()`](/docs/api/yii/base/controller/index.md#method-bindActionParams).

For standalone handlers the typed parameters of `run()` are resolved through the DI container via
[`resolveStandaloneParams()`](/docs/api/yii/base/action/index.md#method-resolveStandaloneParams).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$params` | `array` | The parameters to be bound to the action's `run()` method. |

**Returns**

`mixed`: The result of the action.

**Throws**

- [`InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the action class does not have a `run()` method.

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

```php
public function setModule(yii\base\Module|null $module): void
```

Sets the module that owns this action when invoked standalone.

Used by [`Module::runAction()`](/docs/api/yii/base/module/index.md#method-runAction) when dispatching a handler registered in [`Module::$actionMap`](/docs/api/yii/base/module/index.md#property-actionMap).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$module` | [`Module`](/docs/api/yii/base/module/index.md)`\|null` | The owning module, or `null` to detach. |

**Since**

22.0

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

```php
protected function afterRun(): mixed
```

This method is called right after `run()` is executed.

You may override this method to do post-processing work for the action run.

**Returns**

`mixed`

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

```php
protected function beforeRun(): bool
```

This method is called right before `run()` is executed.

You may override this method to do preparation work for the action run.
If the method returns false, it will cancel the action.

**Returns**

`bool`: Whether to run the action.

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

```php
protected function resolveStandaloneParams(array $params): array<array-key, mixed>
```

Resolves typed parameters of `run()` when the action is invoked without a hosting controller.

Delegates to [`Container::resolveCallableDependencies()`](/docs/api/yii/di/container/index.md#method-resolveCallableDependencies) so that route parameters are matched by name
and class-typed parameters are autowired from the DI container.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$params` | `array` | Route parameters to be matched by name against `run()`'s signature. |

**Returns**

`array<array-key, mixed>`: The resolved positional argument list for `run()`.

**Since**

22.0