Class yii\filters\VerbFilter
Inheritance | yii\filters\VerbFilter » yii\base\Behavior » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable |
Available since version | 2.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'],
],
],
];
}
Public Properties
Property | Type | Description | Defined 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
Method | Description | Defined 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
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'],
]
Method Details
public boolean beforeAction ( $event ) | ||
$event | yii\base\ActionEvent | |
throws | yii\web\MethodNotAllowedHttpException | when the request method is not allowed. |
---|
Declares event handlers for the $owner's events.
public array events ( ) | ||
return | array | Events (array keys) and the corresponding event handler methods (array values). |
---|