# ActionFilter

> ActionFilter is the base class for action filters.

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

| class | `yii\base\ActionFilter` |
| --- | --- |
| Extends | [`Behavior`](/docs/api/yii/base/behavior/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/ActionFilter.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/base/ActionFilter.php#L29) |

9 subclasses:

- [`yii\filters\AccessControl`](/docs/api/yii/filters/accesscontrol/index.md)
- [`yii\filters\AjaxFilter`](/docs/api/yii/filters/ajaxfilter/index.md)
- [`yii\filters\ContentNegotiator`](/docs/api/yii/filters/contentnegotiator/index.md)
- [`yii\filters\Cors`](/docs/api/yii/filters/cors/index.md)
- [`yii\filters\HostControl`](/docs/api/yii/filters/hostcontrol/index.md)
- [`yii\filters\HttpCache`](/docs/api/yii/filters/httpcache/index.md)
- [`yii\filters\PageCache`](/docs/api/yii/filters/pagecache/index.md)
- [`yii\filters\RateLimiter`](/docs/api/yii/filters/ratelimiter/index.md)
- [`yii\filters\auth\AuthMethod`](/docs/api/yii/filters/auth/authmethod/index.md)

ActionFilter is the base class for action filters.

An action filter will participate in the action execution workflow by responding to
the `beforeAction` and `afterAction` events triggered by modules and controllers.

Check implementation of [`AccessControl`](/docs/api/yii/filters/accesscontrol/index.md), [`PageCache`](/docs/api/yii/filters/pagecache/index.md) and [`HttpCache`](/docs/api/yii/filters/httpcache/index.md) as examples on how to use it.

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

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-except"></span>`$except` | `array` | List of action IDs that this filter should not apply to. Default: `[]`. | `ActionFilter` |
| [`$only`](#property-only) | `array` | List of action IDs that this filter should apply to. Default: `[]`. | `ActionFilter` |
| <span id="property-owner"></span>[`$owner`](/docs/api/yii/base/behavior/index.md#property-owner) | `yii\base\T\|null` | The owner of this behavior Default: `null`. | [`Behavior`](/docs/api/yii/base/behavior/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`afterAction()`](#method-afterAction) | This method is invoked right after an action is executed. | `ActionFilter` |
| [`afterFilter()`](#method-afterFilter) |  | `ActionFilter` |
| [`attach()`](#method-attach) | Attaches the behavior object to the component. | `ActionFilter` |
| [`beforeAction()`](#method-beforeAction) | This method is invoked right before an action is to be executed (after all possible filters.) You may override this method to do last-minute preparation for the action. | `ActionFilter` |
| [`beforeFilter()`](#method-beforeFilter) |  | `ActionFilter` |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`BaseObject`](/docs/api/yii/base/baseobject/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) |
| [`detach()`](#method-detach) | Detaches the behavior object from the component. | `ActionFilter` |
| <span id="method-events"></span>[`events()`](/docs/api/yii/base/behavior/index.md#method-events) | Declares event handlers for the [`$owner`](/docs/api/yii/base/behavior/index.md#property-owner)'s events. | [`Behavior`](/docs/api/yii/base/behavior/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`BaseObject`](/docs/api/yii/base/baseobject/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) |
| [`getActionId()`](#method-getActionId) protected | Returns an action ID by converting [`Action::$uniqueId`](/docs/api/yii/base/action/index.md#property-uniqueId) into an ID relative to the module. | `ActionFilter` |
| [`isActive()`](#method-isActive) protected | Returns a value indicating whether the filter is active for the given action. | `ActionFilter` |

## Property details

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

```php
public array $only = []
```

List of action IDs that this filter should apply to.

If this property is not set,
then the filter applies to all actions, unless they are listed in [`$except`](/docs/api/yii/base/actionfilter/index.md#property-except).
If an action ID appears in both [`$only`](/docs/api/yii/base/actionfilter/index.md#property-only) and [`$except`](/docs/api/yii/base/actionfilter/index.md#property-except), this filter will NOT apply to it.

Note that if the filter is attached to a module, the action IDs should also include child module IDs (if any)
and controller IDs.

Since version 2.0.9 action IDs can be specified as wildcards, e.g. `site/*`.

## Method details

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

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

This method is invoked right after an action is executed.

You may override this method to do some postprocessing for the action.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$action` | [`Action`](/docs/api/yii/base/action/index.md) | The action just executed. |
| `$result` | `mixed` | The action execution result |

**Returns**

`mixed`: The processed action result.

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

```php
public function afterFilter(yii\base\ActionEvent $event): mixed
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$event` | [`ActionEvent`](/docs/api/yii/base/actionevent/index.md) |  |

**Returns**

`mixed`

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

```php
public function attach(mixed $owner): mixed
```

Attaches the behavior object to the component.

The default implementation will set the [`$owner`](/docs/api/yii/base/behavior/index.md#property-owner) property
and attach event handlers as declared in [`events()`](/docs/api/yii/base/behavior/index.md#method-events).
Make sure you call the parent implementation if you override this method.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$owner` | `mixed` | The component that this behavior is to be attached to. |

**Returns**

`mixed`

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

```php
public function beforeAction(yii\base\Action $action): bool
```

This method is invoked right before an action is to be executed (after all possible filters.)
You may override this method to do last-minute preparation for the action.

**Parameters**

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

**Returns**

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

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

```php
public function beforeFilter(yii\base\ActionEvent $event): mixed
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$event` | [`ActionEvent`](/docs/api/yii/base/actionevent/index.md) |  |

**Returns**

`mixed`

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

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

Detaches the behavior object from the component.

The default implementation will unset the [`$owner`](/docs/api/yii/base/behavior/index.md#property-owner) property
and detach event handlers declared in [`events()`](/docs/api/yii/base/behavior/index.md#method-events).
Make sure you call the parent implementation if you override this method.

**Returns**

`mixed`

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

```php
protected function getActionId(yii\base\Action $action): string
```

Returns an action ID by converting [`Action::$uniqueId`](/docs/api/yii/base/action/index.md#property-uniqueId) into an ID relative to the module.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$action` | [`Action`](/docs/api/yii/base/action/index.md) |  |

**Returns**

`string`

**Since**

2.0.7

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

```php
protected function isActive(yii\base\Action $action): bool
```

Returns a value indicating whether the filter is active for the given action.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$action` | [`Action`](/docs/api/yii/base/action/index.md) | The action being filtered |

**Returns**

`bool`: Whether the filter is active for the given action.