New controller action
It is very simple to add new actions to the REST API controller. We only need to remember three differences in the web controller:
- Verb setting for the new action
- Authenticate the setting for the new action
- Output for the new action
The first two steps are configured in the behaviors()
method of the controller:
public function behaviors() { $behaviors = parent::behaviors(); $behaviors['verbs'] = [ 'class' => \yii\filters\VerbFilter::className(), 'actions' => [ 'myCustomAction' => ['get', 'head'], ], ]; $behaviors['authenticator'] = [ 'except' => 'myCustomAction', 'class' => HttpBasicAuth::className(), ]; return $behaviors; } public function actionMyCustomAction() { … … }
In the first part of the behaviors()
method, we will only ...
Get Yii2 By Example now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.