# Controller

> Controller is the base class for classes containing controller logic.

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

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

2 subclasses:

- [`yii\console\Controller`](/docs/api/yii/console/controller/index.md)
- [`yii\web\Controller`](/docs/api/yii/web/controller/index.md)

Controller is the base class for classes containing controller logic.

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

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| [`$action`](#property-action) | [`Action`](/docs/api/yii/base/action/index.md)`<static>\|null` | The action that is currently being executed. Default: `null`. | `Controller` |
| <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) |
| [`$defaultAction`](#property-defaultAction) | `string` | The ID of the action that is used when the action ID is not specified in the request. Default: `'index'`. | `Controller` |
| <span id="property-id"></span>`$id` | `string` | The ID of this controller. Default: `null`. | `Controller` |
| [`$layout`](#property-layout) | `string\|null\|false` | The name of the layout to be applied to this controller's views. Default: `null`. | `Controller` |
| <span id="property-module"></span>`$module` | `yii\base\T` | The module that this controller belongs to. Default: `null`. | `Controller` |
| <span id="property-modules"></span>`$modules` read-only | [`Module`](/docs/api/yii/base/module/index.md)`[]` | All ancestor modules that this controller is located within. | `Controller` |
| <span id="property-request"></span>`$request` | [`Request`](/docs/api/yii/base/request/index.md)`\|array\|string` | The request. Default: `'request'`. Available since 2.0.36. | `Controller` |
| <span id="property-response"></span>`$response` | [`Response`](/docs/api/yii/base/response/index.md)`\|array\|string` | The response. Default: `'response'`. Available since 2.0.36. | `Controller` |
| <span id="property-route"></span>`$route` read-only | `string` | The route (module ID, controller ID and action ID) of the current request. | `Controller` |
| <span id="property-uniqueId"></span>`$uniqueId` read-only | `string` | The controller ID that is prefixed with the module ID (if any). | `Controller` |
| <span id="property-view"></span>`$view` | [`View`](/docs/api/yii/base/view/index.md)`\|`[`yii\web\View`](/docs/api/yii/web/view/index.md) | The view object that can be used to render views or view files. | `Controller` |
| <span id="property-viewPath"></span>`$viewPath` | `string` | The directory containing the view files for this controller. | `Controller` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) |  | `Controller` |
| <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) |
| [`actions()`](#method-actions) | Declares external actions for the controller. | `Controller` |
| [`afterAction()`](#method-afterAction) | This method is invoked right after an action is executed. | `Controller` |
| <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 is executed. | `Controller` |
| <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) |
| [`bindActionParams()`](#method-bindActionParams) | Binds the parameters to the action. | `Controller` |
| <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) |
| [`createAction()`](#method-createAction) | Creates an action based on the given action ID. | `Controller` |
| <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) |
| [`findLayoutFile()`](#method-findLayoutFile) | Finds the applicable layout file. | `Controller` |
| <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) |
| [`getModules()`](#method-getModules) | Returns all ancestor modules of this controller. | `Controller` |
| [`getRoute()`](#method-getRoute) | Returns the route of the current request. | `Controller` |
| [`getUniqueId()`](#method-getUniqueId) | Returns the unique ID of the controller. | `Controller` |
| [`getView()`](#method-getView) | Returns the view object that can be used to render views or view files. | `Controller` |
| [`getViewPath()`](#method-getViewPath) | Returns the directory containing view files for this controller. | `Controller` |
| <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) |
| [`init()`](#method-init) | Initializes the object. | `Controller` |
| <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) |
| [`render()`](#method-render) | Renders a view and applies layout if available. | `Controller` |
| [`renderContent()`](#method-renderContent) | Renders a static string by applying a layout. | `Controller` |
| [`renderFile()`](#method-renderFile) | Renders a view file. | `Controller` |
| [`renderPartial()`](#method-renderPartial) | Renders a view without applying layout. | `Controller` |
| [`run()`](#method-run) | Runs a request specified in terms of a route. | `Controller` |
| [`runAction()`](#method-runAction) | Runs an action within this controller with the specified action ID and parameters. | `Controller` |
| [`setView()`](#method-setView) | Sets the view object to be used by this controller. | `Controller` |
| [`setViewPath()`](#method-setViewPath) | Sets the directory that contains the view files. | `Controller` |
| <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) |
| [`bindInjectedParams()`](#method-bindInjectedParams) protected final | Fills parameters based on types and names in action method signature. | `Controller` |

## 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 right before executing a controller action. Event name: `'beforeAction'`. | `Controller` |
| <span id="event-EVENT_AFTER_ACTION"></span>`EVENT_AFTER_ACTION` | [`ActionEvent`](/docs/api/yii/base/actionevent/index.md) | An event raised right after executing a controller action. Event name: `'afterAction'`. | `Controller` |

## Property details

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

```php
public yii\base\Action<static>|null $action = null
```

The action that is currently being executed.

This property will be set
by [`run()`](/docs/api/yii/base/controller/index.md#method-run) when it is called by [`Application`](/docs/api/yii/base/application/index.md) to run an action.

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

```php
public string $defaultAction = 'index'
```

The ID of the action that is used when the action ID is not specified in the request.

Defaults to 'index'.

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

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

The name of the layout to be applied to this controller's views.

This property mainly affects the behavior of [`render()`](/docs/api/yii/base/controller/index.md#method-render).
Defaults to null, meaning the actual layout value should inherit that from [`$module`](/docs/api/yii/base/controller/index.md#property-module)'s layout value.
If false, no layout will be applied.

## Method details

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

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

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$id` | `string` | The ID of this controller. |
| `$module` | `yii\base\T` | The module that this controller belongs to. |
| `$config` | `array<string, mixed>` | Name-value pairs that will be used to initialize the object properties. |

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

```php
public function actions(): array<array-key, class-string|array{class: class-string}>
```

Declares external actions for the controller.

This method is meant to be overwritten to declare external actions for the controller.
It should return an array, with array keys being action IDs, and array values the corresponding
action class names or action configuration arrays. For example,

```
return [
    'action1' => 'app\components\Action1',
    'action2' => [
        'class' => 'app\components\Action2',
        'property1' => 'value1',
        'property2' => 'value2',
    ],
];
```

[`Yii::createObject()`](/docs/api/yii/baseyii/index.md#method-createObject) will be used later to create the requested action
using the configuration provided here.

**Returns**

`array<array-key, class-string|array{class: class-string}>`

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

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

This method is invoked right after an action is executed.

The method will trigger the [`EVENT_AFTER_ACTION`](/docs/api/yii/base/controller/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)`<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<static> $action): bool
```

This method is invoked right before an action is executed.

The method will trigger the [`EVENT_BEFORE_ACTION`](/docs/api/yii/base/controller/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)
{
    // your custom code here, if you want the code to run before action filters,
    // which are triggered on the [[EVENT_BEFORE_ACTION]] event, e.g. PageCache or AccessControl

    if (!parent::beforeAction($action)) {
        return false;
    }

    // other 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)`<static>` | The action to be executed. |

**Returns**

`bool`: Whether the action should continue to run.

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

```php
public function bindActionParams(yii\base\Action<static> $action, array<array-key, mixed> $params): mixed[]
```

Binds the parameters to the action.

This method is invoked by [`Action`](/docs/api/yii/base/action/index.md) when it begins to run with the given parameters.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$action` | [`Action`](/docs/api/yii/base/action/index.md)`<static>` | The action to be bound with parameters. |
| `$params` | `array<array-key, mixed>` | The parameters to be bound to the action. |

**Returns**

`mixed[]`: The valid parameters that the action can run with.

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

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

Creates an action based on the given action ID.

The method first checks if the action ID has been declared in [`actions()`](/docs/api/yii/base/controller/index.md#method-actions). If so,
it will use the configuration declared there to create the action object.
If not, it will look for a controller method whose name is in the format of `actionXyz`
where `xyz` is the action ID. If found, an [`InlineAction`](/docs/api/yii/base/inlineaction/index.md) representing that
method will be created and returned.

**Parameters**

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

**Returns**

[`Action`](/docs/api/yii/base/action/index.md)`<static>|null`: The newly created action instance. Null if the ID doesn't resolve into any action.

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

```php
public function findLayoutFile(yii\base\View $view): string|bool
```

Finds the applicable layout file.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$view` | [`View`](/docs/api/yii/base/view/index.md) | The view object to render the layout file. |

**Returns**

`string|bool`: The layout file path, or false if layout is not needed. Please refer to [`render()`](/docs/api/yii/base/controller/index.md#method-render) on how to specify this parameter.

**Throws**

- [`InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): if an invalid path alias is used to specify the layout.

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

```php
public function getModules(): yii\base\Module[]
```

Returns all ancestor modules of this controller.

The first module in the array is the outermost one (i.e., the application instance),
while the last is the innermost one.

**Returns**

[`Module`](/docs/api/yii/base/module/index.md)`[]`: All ancestor modules that this controller is located within.

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

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

Returns the route of the current request.

**Returns**

`string`: The route (module ID, controller ID and action ID) of the current request.

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

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

Returns the unique ID of the controller.

**Returns**

`string`: The controller ID that is prefixed with the module ID (if any).

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

```php
public function getView(): yii\base\View|yii\web\View
```

Returns the view object that can be used to render views or view files.

The [`render()`](/docs/api/yii/base/controller/index.md#method-render), [`renderPartial()`](/docs/api/yii/base/controller/index.md#method-renderPartial) and [`renderFile()`](/docs/api/yii/base/controller/index.md#method-renderFile) methods will use
this view object to implement the actual view rendering.
If not set, it will default to the "view" application component.

**Returns**

[`View`](/docs/api/yii/base/view/index.md)`|`[`yii\web\View`](/docs/api/yii/web/view/index.md): The view object that can be used to render views or view files.

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

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

Returns the directory containing view files for this controller.

The default implementation returns the directory named as controller [`$id`](/docs/api/yii/base/controller/index.md#property-id) under the [`$module`](/docs/api/yii/base/controller/index.md#property-module)'s
[`$viewPath`](/docs/api/yii/base/controller/index.md#property-viewPath) directory.

**Returns**

`string`: The directory containing the view files for this controller.

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

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

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the
given configuration.

**Returns**

`mixed`

**Since**

2.0.36

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

```php
public function render(string $view, array<string, mixed> $params = []): string
```

Renders a view and applies layout if available.

The view to be rendered can be specified in one of the following formats:

- [path alias](/docs/guide/concept-aliases/index.md) (e.g. "@app/views/site/index");
- absolute path within application (e.g. "//site/index"): the view name starts with double slashes.
  The actual view file will be looked for under the [view path](/docs/api/yii/base/module/index.md#property-viewPath) of the application.
- absolute path within module (e.g. "/site/index"): the view name starts with a single slash.
  The actual view file will be looked for under the [view path](/docs/api/yii/base/module/index.md#property-viewPath) of [`$module`](/docs/api/yii/base/controller/index.md#property-module).
- relative path (e.g. "index"): the actual view file will be looked for under [`$viewPath`](/docs/api/yii/base/controller/index.md#property-viewPath).

To determine which layout should be applied, the following two steps are conducted:

1. In the first step, it determines the layout name and the context module:

- If [`$layout`](/docs/api/yii/base/controller/index.md#property-layout) is specified as a string, use it as the layout name and [`$module`](/docs/api/yii/base/controller/index.md#property-module) as the context module;
- If [`$layout`](/docs/api/yii/base/controller/index.md#property-layout) is null, search through all ancestor modules of this controller and find the first
  module whose [layout](/docs/api/yii/base/module/index.md#property-layout) is not null. The layout and the corresponding module
  are used as the layout name and the context module, respectively. If such a module is not found
  or the corresponding layout is not a string, it will return false, meaning no applicable layout.

2. In the second step, it determines the actual layout file according to the previously found layout name
   and context module. The layout name can be:

- a [path alias](/docs/guide/concept-aliases/index.md) (e.g. "@app/views/layouts/main");
- an absolute path (e.g. "/main"): the layout name starts with a slash. The actual layout file will be
  looked for under the [layout path](/docs/api/yii/base/module/index.md#property-layoutPath) of the application;
- a relative path (e.g. "main"): the actual layout file will be looked for under the
  [layout path](/docs/api/yii/base/module/index.md#property-layoutPath) of the context module.

If the layout name does not contain a file extension, it will use the default one `.php`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$view` | `string` | The view name. |
| `$params` | `array<string, mixed>` | The parameters (name-value pairs) that should be made available in the view. These parameters will not be available in the layout. |

**Returns**

`string`: The rendering result.

**Throws**

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

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

```php
public function renderContent(string $content): string
```

Renders a static string by applying a layout.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$content` | `string` | The static string being rendered |

**Returns**

`string`: The rendering result of the layout with the given static string as the `$content` variable. If the layout is disabled, the string will be returned back.

**Since**

2.0.1

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

```php
public function renderFile(string $file, array<string, mixed> $params = []): string
```

Renders a view file.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$file` | `string` | The view file to be rendered. This can be either a file path or a [path alias](/docs/guide/concept-aliases/index.md). |
| `$params` | `array<string, mixed>` | The parameters (name-value pairs) that should be made available in the view. |

**Returns**

`string`: The rendering result.

**Throws**

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

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

```php
public function renderPartial(string $view, array<string, mixed> $params = []): string
```

Renders a view without applying layout.

This method differs from [`render()`](/docs/api/yii/base/controller/index.md#method-render) in that it does not apply any layout.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$view` | `string` | The view name. Please refer to [`render()`](/docs/api/yii/base/controller/index.md#method-render) on how to specify a view name. |
| `$params` | `array<string, mixed>` | The parameters (name-value pairs) that should be made available in the view. |

**Returns**

`string`: The rendering result.

**Throws**

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

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

```php
public function run(string $route, array<array-key, mixed> $params = []): mixed
```

Runs a request specified in terms of a route.

The route can be either an ID of an action within this controller or a complete route consisting
of module IDs, controller ID and action ID. If the route starts with a slash '/', the parsing of
the route will start from the application; otherwise, it will start from the parent module of this controller.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$route` | `string` | The route to be handled, e.g., 'view', 'comment/view', '/admin/comment/view'. |
| `$params` | `array<array-key, mixed>` | The parameters to be passed to the action. |

**Returns**

`mixed`: The result of the action.

**See also**

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

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

```php
public function runAction(string $id, array<array-key, mixed> $params = []): mixed
```

Runs an action within this controller with the specified action ID and parameters.

If the action ID is empty, the method will use [`$defaultAction`](/docs/api/yii/base/controller/index.md#property-defaultAction).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$id` | `string` | The ID of the action to be executed. |
| `$params` | `array<array-key, mixed>` | The parameters (name-value pairs) to be passed to the action. |

**Returns**

`mixed`: The result of the action.

**Throws**

- [`InvalidRouteException`](/docs/api/yii/base/invalidrouteexception/index.md): if the requested action ID cannot be resolved into an action successfully.

**See also**

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

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

```php
public function setView(yii\base\View|yii\web\View $view): mixed
```

Sets the view object to be used by this controller.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$view` | [`View`](/docs/api/yii/base/view/index.md)`\|`[`yii\web\View`](/docs/api/yii/web/view/index.md) | The view object that can be used to render views or view files. |

**Returns**

`mixed`

### 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

**Since**

2.0.7

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

```php
final protected function bindInjectedParams(ReflectionNamedType $type, string $name, array &$args, array &$requestedParams): mixed
```

Fills parameters based on types and names in action method signature.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$type` | [`ReflectionNamedType`](https://www.php.net/manual/en/class.reflectionnamedtype.php) | The reflected type of the action parameter. |
| `$name` | `string` | The name of the parameter. |
| `$args` | `array` | The array of arguments for the action, this function may append items to it. |
| `$requestedParams` | `array` | The array with requested params, this function may write specific keys to it. |

**Returns**

`mixed`

**Throws**

- [`ErrorException`](/docs/api/yii/base/errorexception/index.md): when we cannot load a required service.
- [`InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): Thrown when there is an error in the DI configuration.
- [`yii\di\NotInstantiableException`](/docs/api/yii/di/notinstantiableexception/index.md): Thrown when a definition cannot be resolved to a concrete class (for example an interface type hint) without a proper definition in the container.

**Since**

2.0.36