How to perform additional tasks in a Yii2 restful controller?

This is what my RESTful controller looks like.

<?php namespace backend\controllers; use yii\rest\Controller; use yii; use yii\web\Response; use yii\helpers\ArrayHelper; class UserController extends \yii\rest\ActiveController { public function behaviors() { return ArrayHelper::merge(parent::behaviors(), [ [ 'class' => 'yii\filters\ContentNegotiator', 'only' => ['view', 'index'], // in a controller // if in a module, use the following IDs for user actions // 'only' => ['user/view', 'user/index'] 'formats' => [ 'application/json' => Response::FORMAT_JSON, ], 'languages' => [ 'en', 'de', ], ], [ 'class' => \yii\filters\Cors::className(), 'cors' => [ 'Origin' => ['*'], 'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'], 'Access-Control-Request-Headers' => ['*'], 'Access-Control-Allow-Credentials' => true, 'Access-Control-Max-Age' => 86400, ], ], ]); } public $modelClass = 'backend\models\User'; public function actions() { } public function sendMail(){ //Need to call this function on every create //This should also have the information about the newly created user } } 

It works great with default behavior, but not very practical that you just create a user and exit. You need to send an email with confirmation of the SMS link, etc., Some other models based on this action may be updated.

I do not want to completely override the create method, since it works well for saving data and returning JSON. I just want to expand the functionality by adding a function callback function that can accept a newly created user and send an email to this person.

+5
source share
2 answers

Take a look here: https://github.com/githubjeka/yii2-rest/blob/bf034d26f90faa3023e5831d1eb165854c5c7aaf/rest/versions/v1/controllers/PostController.php

As you can see, this is using prepareDataProvider to change the usual way of using the index. It is very comfortable. You can find the definition of prepareDataProvider here: http://www.yiiframework.com/doc-2.0/yii-rest-indexaction.html#prepareDataProvider()-detail

Now, as you can see, there are two additional methods afterRun () and beforeRun (), which are also available for the create action. http://www.yiiframework.com/doc-2.0/yii-rest-createaction.html

You can use these 2 functions and make them look like prepareDataProvider to do more things like send email. I have not tried them myself, but I think that this should be the way.

+3
source

The easiest way is to take advantage of the afterSave() method in your model. This method will be called after each save process.

 public function afterSave($insert, $changedAttributes) { //calling a send mail function return parent::afterSave($insert, $changedAttributes); } 

Another advantage of this method is the data that you saved in your object model. For example, to access the email field:

  public function afterSave($insert, $changedAttributes) { //calling a send mail function \app\helpers\EmailHelper::send($this->email); return parent::afterSave($insert, $changedAttributes); } 

the value of $this->email contains the save value in the database.

Note You can use $this->isNewRecord to determine if the model is saving a new record to the database or updating an existing record. Take a look:

 public function afterSave($insert, $changedAttributes) { if($this->isNewRecord){ //calling a send mail function \app\helpers\EmailHelper::send(**$this->email**); } return parent::afterSave($insert, $changedAttributes); } 

Now it sends mail only if the new record is stored in the database.

Please note that you can also use Yii2 EVENTS .

Like the official Yii2 documentation :

This method is called at the end of the insert or update record. The default implementation will raise the EVENT_AFTER_INSERT event when $ insert is true or the EVENT_AFTER_UPDATE event if $ insert is false. The event class used is yii\db\AfterSaveEvent . When overriding this method, be sure to call the parent implementation so that the event fires.

+2
source

Source: https://habr.com/ru/post/1233959/


All Articles