0 follower

Class yii\filters\VerbFilter

Inheritanceyii\filters\VerbFilter » yii\base\Behavior » yii\base\BaseObject
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 https://tools.ietf.org/html/rfc2616#section-14.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|null 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\BaseObject
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
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\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
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\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\base\BaseObject

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).