Class yii\filters\VerbFilter

Inheritanceyii\filters\VerbFilter » yii\base\Behavior » yii\base\Object
Implementsyii\base\Configurable
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/filters/VerbFilter.php

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::className(),
            'actions' => [
                'index'  => ['get'],
                'view'   => ['get'],
                'create' => ['get', 'post'],
                'update' => ['get', 'put', 'post'],
                'delete' => ['post', 'delete'],
            ],
        ],
    ];
}

See also http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.7.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$actions array This property defines the allowed request methods for each action. yii\filters\VerbFilter
$owner yii\base\Component The owner of this behavior yii\base\Behavior

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Object
__construct() Constructor. yii\base\Object
__get() Returns the value of an object property. yii\base\Object
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Object
__set() Sets value of an object property. yii\base\Object
__unset() Sets an object property to null. yii\base\Object
attach() Attaches the behavior object to the component. yii\base\Behavior
beforeAction() yii\filters\VerbFilter
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Object
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Object
className() Returns the fully qualified name of this class. yii\base\Object
detach() Detaches the behavior object from the component. yii\base\Behavior
events() Declares event handlers for the $owner's events. yii\filters\VerbFilter
hasMethod() Returns a value indicating whether a method is defined. yii\base\Object
hasProperty() Returns a value indicating whether a property is defined. yii\base\Object
init() Initializes the object. yii\base\Object

Property Details

$actions public property

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 '*'.

For example,

[
  'create' => ['get', 'post'],
  'update' => ['get', 'put', 'post'],
  'delete' => ['post', 'delete'],
  '*' => ['get'],
]
public array $actions = []

Method Details

beforeAction() public method

public boolean beforeAction ( $event )
$event yii\base\ActionEvent
throws yii\web\MethodNotAllowedHttpException

when the request method is not allowed.

events() public method

Declares event handlers for the $owner's events.

public array events ( )
return array

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