# ActiveController

> ActiveController implements a common set of actions for supporting RESTful access to ActiveRecord.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [rest](/docs/api/yii/rest/index.md) › ActiveController

| class | `yii\rest\ActiveController` |
| --- | --- |
| Extends | [`Controller`](/docs/api/yii/rest/controller/index.md) » [`yii\web\Controller`](/docs/api/yii/web/controller/index.md) » [`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/rest/ActiveController.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/rest/ActiveController.php#L45) |

ActiveController implements a common set of actions for supporting RESTful access to ActiveRecord.

The class of the ActiveRecord should be specified via [`$modelClass`](/docs/api/yii/rest/activecontroller/index.md#property-modelClass), which must implement [`ActiveRecordInterface`](/docs/api/yii/db/activerecordinterface/index.md).
By default, the following actions are supported:

- `index`: list of models
- `view`: return the details of a model
- `create`: create a new model
- `update`: update an existing model
- `delete`: delete an existing model
- `options`: return the allowed HTTP methods

You may disable some of these actions by overriding [`actions()`](/docs/api/yii/rest/activecontroller/index.md#method-actions) and unsetting the corresponding actions.

To add a new action, either override [`actions()`](/docs/api/yii/rest/activecontroller/index.md#method-actions) by appending a new action class or write a new action method.
Make sure you also override [`verbs()`](/docs/api/yii/rest/activecontroller/index.md#method-verbs) to properly declare what HTTP methods are allowed by the new action.

You should usually override [`checkAccess()`](/docs/api/yii/rest/activecontroller/index.md#method-checkAccess) to check whether the current user has the privilege to perform
the specified action against the specified model.

For more details and usage information on ActiveController, see the [guide article on rest controllers](/docs/guide/rest-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`](/docs/api/yii/web/controller/index.md#property-actionParams) | `array` | The parameters bound to the current action. Default: `[]`. | [`yii\web\Controller`](/docs/api/yii/web/controller/index.md) |
| <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-createScenario"></span>`$createScenario` | `string` | The scenario used for creating a model. Default: `\yii\base\Model::SCENARIO_DEFAULT`. | `ActiveController` |
| <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) |
| <span id="property-enableCsrfValidation"></span>[`$enableCsrfValidation`](/docs/api/yii/rest/controller/index.md#property-enableCsrfValidation) | `bool` | Default: `false`. | [`Controller`](/docs/api/yii/rest/controller/index.md) |
| <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) |
| [`$modelClass`](#property-modelClass) | `string` | The model class name. Default: `null`. | `ActiveController` |
| <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`](/docs/api/yii/web/controller/index.md#property-request) | [`yii\web\Request`](/docs/api/yii/web/request/index.md) | The request object. | [`yii\web\Controller`](/docs/api/yii/web/controller/index.md) |
| <span id="property-response"></span>[`$response`](/docs/api/yii/web/controller/index.md#property-response) | [`yii\web\Response`](/docs/api/yii/web/response/index.md) | The response object. | [`yii\web\Controller`](/docs/api/yii/web/controller/index.md) |
| <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-serializer"></span>[`$serializer`](/docs/api/yii/rest/controller/index.md#property-serializer) | `string\|array` | The configuration for creating the serializer that formats the response data. Default: `'yii\rest\Serializer'`. | [`Controller`](/docs/api/yii/rest/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-updateScenario"></span>`$updateScenario` | `string` | The scenario used for updating a model. Default: `\yii\base\Model::SCENARIO_DEFAULT`. | `ActiveController` |
| <span id="property-view"></span>[`$view`](/docs/api/yii/web/controller/index.md#property-view) | [`yii\web\View`](/docs/api/yii/web/view/index.md) | The view object that can be used to render views or view files. | [`yii\web\Controller`](/docs/api/yii/web/controller/index.md) |
| <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) |
| [`actions()`](#method-actions) | Declares external actions for the controller. | `ActiveController` |
| <span id="method-afterAction"></span>[`afterAction()`](/docs/api/yii/rest/controller/index.md#method-afterAction) | This method is invoked right after an action is executed. | [`Controller`](/docs/api/yii/rest/controller/index.md) |
| <span id="method-asJson"></span>[`asJson()`](/docs/api/yii/web/controller/index.md#method-asJson) | Send data formatted as JSON. | [`yii\web\Controller`](/docs/api/yii/web/controller/index.md) |
| <span id="method-asXml"></span>[`asXml()`](/docs/api/yii/web/controller/index.md#method-asXml) | Send data formatted as XML. | [`yii\web\Controller`](/docs/api/yii/web/controller/index.md) |
| <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) |
| <span id="method-beforeAction"></span>[`beforeAction()`](/docs/api/yii/web/controller/index.md#method-beforeAction) | This method is invoked right before an action is executed. | [`yii\web\Controller`](/docs/api/yii/web/controller/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/rest/controller/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`Controller`](/docs/api/yii/rest/controller/index.md) |
| <span id="method-bindActionParams"></span>[`bindActionParams()`](/docs/api/yii/web/controller/index.md#method-bindActionParams) | Binds the parameters to the action. | [`yii\web\Controller`](/docs/api/yii/web/controller/index.md) |
| <span id="method-bindActionParamsToCallable"></span>[`bindActionParamsToCallable()`](/docs/api/yii/web/controller/index.md#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). | [`yii\web\Controller`](/docs/api/yii/web/controller/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. | [`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) |
| [`checkAccess()`](#method-checkAccess) | Checks the privilege of the current user. | `ActiveController` |
| <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) |
| <span id="method-goBack"></span>[`goBack()`](/docs/api/yii/web/controller/index.md#method-goBack) | Redirects the browser to the last visited page. | [`yii\web\Controller`](/docs/api/yii/web/controller/index.md) |
| <span id="method-goHome"></span>[`goHome()`](/docs/api/yii/web/controller/index.md#method-goHome) | Redirects the browser to the home page. | [`yii\web\Controller`](/docs/api/yii/web/controller/index.md) |
| <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) |
| [`init()`](#method-init) | Initializes the object. | `ActiveController` |
| <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) |
| <span id="method-redirect"></span>[`redirect()`](/docs/api/yii/web/controller/index.md#method-redirect) | Redirects the browser to the specified URL. | [`yii\web\Controller`](/docs/api/yii/web/controller/index.md) |
| <span id="method-refresh"></span>[`refresh()`](/docs/api/yii/web/controller/index.md#method-refresh) | Refreshes the current page. | [`yii\web\Controller`](/docs/api/yii/web/controller/index.md) |
| <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) |
| <span id="method-renderAjax"></span>[`renderAjax()`](/docs/api/yii/web/controller/index.md#method-renderAjax) | Renders a view in response to an AJAX request. | [`yii\web\Controller`](/docs/api/yii/web/controller/index.md) |
| <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) |
| <span id="method-serializeData"></span>[`serializeData()`](/docs/api/yii/rest/controller/index.md#method-serializeData) protected | Serializes the specified data. | [`Controller`](/docs/api/yii/rest/controller/index.md) |
| [`verbs()`](#method-verbs) protected | Declares the allowed HTTP verbs. | `ActiveController` |

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

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

```php
public string $modelClass = null
```

The model class name.

This property must be set.

## Method details

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

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

```php
public function checkAccess(string $action, object|null $model = null, array $params = []): mixed
```

Checks the privilege of the current user.

This method should be overridden to check whether the current user has the privilege
to run the specified action against the specified data model.
If the user does not have access, a [`ForbiddenHttpException`](/docs/api/yii/web/forbiddenhttpexception/index.md) should be thrown.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$action` | `string` | The ID of the action to be executed |
| `$model` | `object\|null` | The model to be accessed. If null, it means no specific model is being accessed. |
| `$params` | `array` | Additional parameters |

**Returns**

`mixed`

**Throws**

- [`yii\web\ForbiddenHttpException`](/docs/api/yii/web/forbiddenhttpexception/index.md): if the user does not have access

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

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

```php
protected function verbs(): array
```

Declares the allowed HTTP verbs.

Please refer to [`VerbFilter::$actions`](/docs/api/yii/filters/verbfilter/index.md#property-actions) on how to declare the allowed verbs.

**Returns**

`array`: The allowed HTTP verbs.