I’m new in Yii2 REST API and i have following scenario:
I have User model with UserController.
UserController has many actions with update actions like activate, deactivate, placeInBlackbox, updatePassword, and other actions which update record partially and manage relations of record.
I learned that it can be resolved by PATCH method setting operation ID like the following:
Activate/deactivate is changing status so these both are regular POST. updatePassword as well. Not sure what placeInBlackbox but I think you should have a separate blackbox endpoint for it.
As I understand there is no opportunity to call separate action via PATCH method? Yes, activate/deactivate can be made by regular POST/PUT sending columns in bodyParams. But i have actions which make multiple operations besides update record like (it just example):
public function actionPlaceInBlackBox() {
$user = $this->findModel(Yii::$app->user->id);
$user->deactivate(); //change enable to false
$blackBox = new BlackBox();
$blackBox->create($user->id); //create BlackBox Model
Log::process(Log::EVENT_PLACE_IN_BLACKBOX, $user->id); //create Log;
return ['status_code' => 200];
}
this action can be executed by
PUT /users/123/do/placeinblackbox
and it works. But i’m not sure that it’s correct according to REST API best practices (Use nouns but no verbs).
Is there any ways to call separate action according to recommendation above? I’ve read one article (unfortunately i can’t paste link according forum policy) about PATCH method, where it is possible to send operation_id via bodyParams: