# VerbFilter

> VerbFilter is an action filter that filters by HTTP request methods.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [filters](/docs/api/yii/filters/index.md) › VerbFilter

| class | `yii\filters\VerbFilter` |
| --- | --- |
| Extends | [`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/filters/VerbFilter.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/filters/VerbFilter.php#L52) |

VerbFilter is an action filter that filters by HTTP request methods.

It allows to define allowed HTTP request methods for each action and will throw
an HTTP 405 error when the method is not allowed.

To use VerbFilter, declare it in the `behaviors()` method of your controller class.
For example, the following declarations will define a typical set of allowed
request methods for REST CRUD actions.

```
public function behaviors()
{
    return [
        'verbs' => [
            'class' => \yii\filters\VerbFilter::class,
            'actions' => [
                'index'  => ['GET'],
                'view'   => ['GET'],
                'create' => ['GET', 'POST'],
                'update' => ['GET', 'PUT', 'POST'],
                'delete' => ['POST', 'DELETE'],
            ],
        ],
    ];
}
```

See also: <https://tools.ietf.org/html/rfc2616#section-14.7>.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| [`$actions`](#property-actions) | `array` | This property defines the allowed request methods for each action. Default: `[]`. | `VerbFilter` |
| <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`. | [`yii\base\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. | [`yii\base\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. | [`yii\base\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. | [`yii\base\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. | [`yii\base\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. | [`yii\base\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. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-attach"></span>[`attach()`](/docs/api/yii/base/behavior/index.md#method-attach) | Attaches the behavior object to the component. | [`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md) |
| [`beforeAction()`](#method-beforeAction) |  | `VerbFilter` |
| <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. | [`yii\base\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. | [`yii\base\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. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-detach"></span>[`detach()`](/docs/api/yii/base/behavior/index.md#method-detach) | Detaches the behavior object from the component. | [`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md) |
| [`events()`](#method-events) | Declares event handlers for the [`$owner`](/docs/api/yii/base/behavior/index.md#property-owner)'s events. | `VerbFilter` |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\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. | [`yii\base\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. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |

## Property details

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

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

This property defines the allowed request methods for each action.

For each action that should only support limited set of request methods
you add an entry with the action id as array key and an array of
allowed methods (e.g. GET, HEAD, PUT) as the value.
If an action is not listed all request methods are considered allowed.

You can use `'*'` to stand for all actions. When an action is explicitly
specified, it takes precedence over the specification given by `'*'`.

## Method details

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

```php
public function beforeAction(yii\base\ActionEvent $event): bool
```

**Parameters**

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

**Returns**

`bool`

**Throws**

- [`yii\web\MethodNotAllowedHttpException`](/docs/api/yii/web/methodnotallowedhttpexception/index.md): when the request method is not allowed.

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

```php
public function events(): array
```

Declares event handlers for the [`$owner`](/docs/api/yii/base/behavior/index.md#property-owner)'s events.

**Returns**

`array`: Events (array keys) and the corresponding event handler methods (array values).