# Controller

> Controller is the base class of web controllers.

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

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

2 subclasses:

- [`yii\rest\Controller`](/docs/api/yii/rest/controller/index.md)
- [`yii\inertia\web\Controller`](/docs/extensions/inertia/reference/web/controller/index.md)

Controller is the base class of web controllers.

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 |
| --- | --- | --- | --- |
| <span id="property-action"></span>[`$action`](/docs/api/yii/base/controller/index.md#property-action) | [`yii\base\Action`](/docs/api/yii/base/action/index.md)`<static>\|null` | The action that is currently being executed. Default: `null`. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="property-actionParams"></span>`$actionParams` | `array` | The parameters bound to the current action. Default: `[]`. | `Controller` |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="property-defaultAction"></span>[`$defaultAction`](/docs/api/yii/base/controller/index.md#property-defaultAction) | `string` | The ID of the action that is used when the action ID is not specified in the request. Default: `'index'`. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| [`$enableCsrfValidation`](#property-enableCsrfValidation) | `bool` | Whether to enable CSRF validation for the actions in this controller. Default: `true`. | `Controller` |
| <span id="property-id"></span>[`$id`](/docs/api/yii/base/controller/index.md#property-id) | `string` | The ID of this controller. Default: `null`. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="property-layout"></span>[`$layout`](/docs/api/yii/base/controller/index.md#property-layout) | `string\|null\|false` | The name of the layout to be applied to this controller's views. Default: `null`. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="property-module"></span>[`$module`](/docs/api/yii/base/controller/index.md#property-module) | `yii\base\T` | The module that this controller belongs to. Default: `null`. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="property-modules"></span>[`$modules`](/docs/api/yii/base/controller/index.md#property-modules) read-only | [`yii\base\Module`](/docs/api/yii/base/module/index.md)`[]` | All ancestor modules that this controller is located within. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="property-request"></span>`$request` | [`Request`](/docs/api/yii/web/request/index.md) | The request object. | `Controller` |
| <span id="property-response"></span>`$response` | [`Response`](/docs/api/yii/web/response/index.md) | The response object. | `Controller` |
| <span id="property-route"></span>[`$route`](/docs/api/yii/base/controller/index.md#property-route) read-only | `string` | The route (module ID, controller ID and action ID) of the current request. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="property-uniqueId"></span>[`$uniqueId`](/docs/api/yii/base/controller/index.md#property-uniqueId) read-only | `string` | The controller ID that is prefixed with the module ID (if any). | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="property-view"></span>`$view` | [`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`](/docs/api/yii/base/controller/index.md#property-viewPath) | `string` | The directory containing the view files for this controller. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/controller/index.md#method-__construct) |  | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <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. | [`yii\base\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. | [`yii\base\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. | [`yii\base\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. | [`yii\base\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. | [`yii\base\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. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-actions"></span>[`actions()`](/docs/api/yii/base/controller/index.md#method-actions) | Declares external actions for the controller. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="method-afterAction"></span>[`afterAction()`](/docs/api/yii/base/controller/index.md#method-afterAction) | This method is invoked right after an action is executed. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| [`asJson()`](#method-asJson) | Send data formatted as JSON. | `Controller` |
| [`asXml()`](#method-asXml) | Send data formatted as XML. | `Controller` |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\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. | [`yii\base\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. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`bindActionParams()`](#method-bindActionParams) | Binds the parameters to the action. | `Controller` |
| [`bindActionParamsToCallable()`](#method-bindActionParamsToCallable) static | Resolves the parameters for an action's invokable callable using the same scalar coercion, type validation, and DI rules as [`bindActionParams()`](/docs/api/yii/web/controller/index.md#method-bindActionParams). | `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. | [`yii\base\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. | [`yii\base\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. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-createAction"></span>[`createAction()`](/docs/api/yii/base/controller/index.md#method-createAction) | Creates an action based on the given action ID. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\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. | [`yii\base\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. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-findLayoutFile"></span>[`findLayoutFile()`](/docs/api/yii/base/controller/index.md#method-findLayoutFile) | Finds the applicable layout file. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\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. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getModules"></span>[`getModules()`](/docs/api/yii/base/controller/index.md#method-getModules) | Returns all ancestor modules of this controller. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="method-getRoute"></span>[`getRoute()`](/docs/api/yii/base/controller/index.md#method-getRoute) | Returns the route of the current request. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="method-getUniqueId"></span>[`getUniqueId()`](/docs/api/yii/base/controller/index.md#method-getUniqueId) | Returns the unique ID of the controller. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="method-getView"></span>[`getView()`](/docs/api/yii/base/controller/index.md#method-getView) | Returns the view object that can be used to render views or view files. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="method-getViewPath"></span>[`getViewPath()`](/docs/api/yii/base/controller/index.md#method-getViewPath) | Returns the directory containing view files for this controller. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| [`goBack()`](#method-goBack) | Redirects the browser to the last visited page. | `Controller` |
| [`goHome()`](#method-goHome) | Redirects the browser to the home page. | `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. | [`yii\base\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. | [`yii\base\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. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/controller/index.md#method-init) | Initializes the object. | [`yii\base\Controller`](/docs/api/yii/base/controller/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. | [`yii\base\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. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`redirect()`](#method-redirect) | Redirects the browser to the specified URL. | `Controller` |
| [`refresh()`](#method-refresh) | Refreshes the current page. | `Controller` |
| <span id="method-render"></span>[`render()`](/docs/api/yii/base/controller/index.md#method-render) | Renders a view and applies layout if available. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| [`renderAjax()`](#method-renderAjax) | Renders a view in response to an AJAX request. | `Controller` |
| <span id="method-renderContent"></span>[`renderContent()`](/docs/api/yii/base/controller/index.md#method-renderContent) | Renders a static string by applying a layout. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="method-renderFile"></span>[`renderFile()`](/docs/api/yii/base/controller/index.md#method-renderFile) | Renders a view file. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="method-renderPartial"></span>[`renderPartial()`](/docs/api/yii/base/controller/index.md#method-renderPartial) | Renders a view without applying layout. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="method-run"></span>[`run()`](/docs/api/yii/base/controller/index.md#method-run) | Runs a request specified in terms of a route. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="method-runAction"></span>[`runAction()`](/docs/api/yii/base/controller/index.md#method-runAction) | Runs an action within this controller with the specified action ID and parameters. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="method-setView"></span>[`setView()`](/docs/api/yii/base/controller/index.md#method-setView) | Sets the view object to be used by this controller. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="method-setViewPath"></span>[`setViewPath()`](/docs/api/yii/base/controller/index.md#method-setViewPath) | Sets the directory that contains the view files. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-bindInjectedParams"></span>[`bindInjectedParams()`](/docs/api/yii/base/controller/index.md#method-bindInjectedParams) protected final | Fills parameters based on types and names in action method signature. | [`yii\base\Controller`](/docs/api/yii/base/controller/index.md) |

## Events

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

## Property details

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

```php
public bool $enableCsrfValidation = true
```

Whether to enable CSRF validation for the actions in this controller.

CSRF validation is enabled only when both this property and [`Request::$enableCsrfValidation`](/docs/api/yii/web/request/index.md#property-enableCsrfValidation) are true.

## Method details

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

```php
public function asJson(mixed $data): yii\web\Response
```

Send data formatted as JSON.

This method is a shortcut for sending data formatted as JSON. It will return
the [response](/docs/api/yii/web/application/index.md#method-getResponse) application component after configuring
the [format](/docs/api/yii/web/response/index.md#property-format) and setting the [data](/docs/api/yii/web/response/index.md#property-data) that should
be formatted. A common usage will be:

```
return $this->asJson($data);
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$data` | `mixed` | The data that should be formatted. |

**Returns**

[`Response`](/docs/api/yii/web/response/index.md): A response that is configured to send `$data` formatted as JSON.

**Since**

2.0.11

**See also**

- [`Response::$format`](/docs/api/yii/web/response/index.md#property-format)
- [`Response::FORMAT_JSON`](/docs/api/yii/web/response/index.md#const-FORMAT_JSON)
- [`JsonResponseFormatter`](/docs/api/yii/web/jsonresponseformatter/index.md)

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

```php
public function asXml(mixed $data): yii\web\Response
```

Send data formatted as XML.

This method is a shortcut for sending data formatted as XML. It will return
the [response](/docs/api/yii/web/application/index.md#method-getResponse) application component after configuring
the [format](/docs/api/yii/web/response/index.md#property-format) and setting the [data](/docs/api/yii/web/response/index.md#property-data) that should
be formatted. A common usage will be:

```
return $this->asXml($data);
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$data` | `mixed` | The data that should be formatted. |

**Returns**

[`Response`](/docs/api/yii/web/response/index.md): A response that is configured to send `$data` formatted as XML.

**Since**

2.0.11

**See also**

- [`Response::$format`](/docs/api/yii/web/response/index.md#property-format)
- [`Response::FORMAT_XML`](/docs/api/yii/web/response/index.md#const-FORMAT_XML)
- [`XmlResponseFormatter`](/docs/api/yii/web/xmlresponseformatter/index.md)

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

```php
public function beforeAction(mixed $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` | `mixed` | 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.

This method will check the parameter names that the action requires and return the provided parameters according
to the requirement. If there is any missing parameter, an exception will be thrown.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$action` | [`yii\base\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.

**Throws**

- [`BadRequestHttpException`](/docs/api/yii/web/badrequesthttpexception/index.md): if there are missing or invalid parameters.

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

```php
public static function bindActionParamsToCallable(ReflectionMethod $method, array $params, yii\base\Module|null $module = null): array
```

Resolves the parameters for an action's invokable callable using the same scalar coercion, type validation,
and DI rules as [`bindActionParams()`](/docs/api/yii/web/controller/index.md#method-bindActionParams).

Standalone actions extending [`Action`](/docs/api/yii/base/action/index.md) use this static entry point so they get the exact same
HTTP-aware parameter handling as controller-hosted actions.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$method` | [`ReflectionMethod`](https://www.php.net/manual/en/class.reflectionmethod.php) | The reflected `run()` (or controller method) whose parameters are being bound. |
| `$params` | `array` | Route parameters available for binding (typically `$_GET`/`$_POST`-derived). |
| `$module` | [`yii\base\Module`](/docs/api/yii/base/module/index.md)`\|null` | The module that should be queried for components and DI definitions when resolving non-builtin typed parameters. May be `null` for standalone resolution outside a module context. |

**Returns**

`array`: A tuple `[positional args, named action params, named requested params]` where the first item is passed to the callable, the second mirrors the controller's `actionParams`, and the third is a description map for `Yii::$app->requestedParams`.

**Throws**

- [`BadRequestHttpException`](/docs/api/yii/web/badrequesthttpexception/index.md): if a scalar parameter fails type coercion or a required parameter is missing.
- [`ServerErrorHttpException`](/docs/api/yii/web/servererrorhttpexception/index.md): if a typed dependency cannot be resolved by the container.

**Since**

22.0

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

```php
public function goBack(string|array|null $defaultUrl = null): yii\web\Response
```

Redirects the browser to the last visited page.

You can use this method in an action by returning the [`Response`](/docs/api/yii/web/response/index.md) directly:

```
// stop executing this action and redirect to last visited page
return $this->goBack();
```

For this function to work you have to [set the return URL](/docs/api/yii/web/user/index.md#method-setReturnUrl) in appropriate places before.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$defaultUrl` | `string\|array\|null` | The default return URL in case it was not set previously. If this is null and the return URL was not set previously, [`Application::$homeUrl`](/docs/api/yii/web/application/index.md#property-homeUrl) will be redirected to. Please refer to [`User::setReturnUrl()`](/docs/api/yii/web/user/index.md#method-setReturnUrl) on accepted format of the URL. |

**Returns**

[`Response`](/docs/api/yii/web/response/index.md): The current response object

**See also**

- [`User::getReturnUrl()`](/docs/api/yii/web/user/index.md#method-getReturnUrl)

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

```php
public function goHome(): yii\web\Response
```

Redirects the browser to the home page.

You can use this method in an action by returning the [`Response`](/docs/api/yii/web/response/index.md) directly:

```
// stop executing this action and redirect to home page
return $this->goHome();
```

**Returns**

[`Response`](/docs/api/yii/web/response/index.md): The current response object

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

```php
public function redirect(string|array $url, int $statusCode = 302): yii\web\Response
```

Redirects the browser to the specified URL.

This method is a shortcut to [`Response::redirect()`](/docs/api/yii/web/response/index.md#method-redirect).

You can use it in an action by returning the [`Response`](/docs/api/yii/web/response/index.md) directly:

```
// stop executing this action and redirect to login page
return $this->redirect(['login']);
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$url` | `string\|array` | The URL to be redirected to. This can be in one of the following formats: - a string representing a URL (e.g. "https://example.com") - a string representing a URL alias (e.g. "@example.com") - an array in the format of `[$route, ...name-value pairs...]` (e.g. `['site/index', 'ref' => 1]`) [`Url::to()`](/docs/api/yii/helpers/baseurl/index.md#method-to) will be used to convert the array into a URL. Any relative URL that starts with a single forward slash "/" will be converted into an absolute one by prepending it with the host info of the current request. |
| `$statusCode` | `int` | The HTTP status code. Defaults to 302. See <https://tools.ietf.org/html/rfc2616#section-10> for details about HTTP status code |

**Returns**

[`Response`](/docs/api/yii/web/response/index.md): The current response object

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

```php
public function refresh(string $anchor = ''): yii\web\Response
```

Refreshes the current page.

This method is a shortcut to [`Response::refresh()`](/docs/api/yii/web/response/index.md#method-refresh).

You can use it in an action by returning the [`Response`](/docs/api/yii/web/response/index.md) directly:

```
// stop executing this action and refresh the current page
return $this->refresh();
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$anchor` | `string` | The anchor that should be appended to the redirection URL. Defaults to empty. Make sure the anchor starts with '#' if you want to specify it. |

**Returns**

[`Response`](/docs/api/yii/web/response/index.md): The response object itself

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

```php
public function renderAjax(string $view, array $params = []): string
```

Renders a view in response to an AJAX request.

This method is similar to [`renderPartial()`](/docs/api/yii/base/controller/index.md#method-renderPartial) except that it will inject into
the rendering result with JS/CSS scripts and files which are registered with the view.
For this reason, you should use this method instead of [`renderPartial()`](/docs/api/yii/base/controller/index.md#method-renderPartial) to render
a view to respond to an AJAX request.

**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` | The parameters (name-value pairs) that should be made available in the view. |

**Returns**

`string`: The rendering result.