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() { … … ...