2015-10-18 60 views
5

這是我的RESTful控制器的樣子。如何在Yii2寧靜控制器中執行其他任務?

<?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(){ 
 
\t //Need to call this function on every create 
 
\t //This should also have the information about the newly created user 
 
    } 
 

 
    }

它的工作原理非常好,默認的行爲,但它是不是很實用,你將只需要創建用戶並退出。你需要發送電子郵件與驗證鏈接短信等,可能會更新一些其他模式的基礎上,這一行動。

我不想完全重寫create方法,因爲它很好地保存數據並返回JSON。 我只想通過添加一個函數的回調類來擴展它的功能,該函數可以接受新創建的用戶併發送電子郵件給該人。

回答

2

最簡單的方法是從模型中的afterSave()方法中獲益。此方法將在每個保存過程之後調用。

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

此方法的另一個優點是您存儲在對象模型中的數據。例如訪問email字段:

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

$this->email的值是包含保存值到數據庫中。

注意 您可以從$this->isNewRecord受益檢測模型是否是節能新記錄到數據庫或更新現有記錄。看看:

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

現在,它只發送郵件,如果新的記錄被保存到數據庫。

請注意,您也可以受益於Yii2的EVENTS

As official Yii2's documentation

這種方法被稱爲在插入或更新記錄的末尾。 當$ insert爲true時,默認實現將觸發EVENT_AFTER_INSERT事件,如果$ insert爲false,則默認實現將觸發EVENT_AFTER_UPDATE事件。使用的事件類別是yii\db\AfterSaveEvent。重寫此方法時,請確保調用父實現,以便觸發事件。

+0

我已經在我的代碼中添加了這個函數,但它沒有被調用,我將不得不寫一些東西來覆蓋它嗎? – Viky293

+0

@ Viky293這是你的'Model'類中的一個方法。我的意思是:'後端\模型\用戶'。您必須在'backend \ models \ User'類中覆蓋它。 –

+0

感謝您的幫助,但我不希望將代碼發送到模型內部。我用afterAction()函數嘗試過,就像你已經解釋的那樣,但是我也沒有找到一個好的解決方案,因爲afterAction將在所有控制器動作之後被調用。 – Viky293

3

到這裏看看:https://github.com/githubjeka/yii2-rest/blob/bf034d26f90faa3023e5831d1eb165854c5c7aaf/rest/versions/v1/controllers/PostController.php

正如你可以看到這是使用prepareDataProvider更改索引動作用正常的方式。這非常方便。您可以在此處找到prepareDataProvider的定義:http://www.yiiframework.com/doc-2.0/yii-rest-indexaction.html#prepareDataProvider()-detail

現在,您可以看到在執行創建操作時也可以使用afterRun()和beforeRun()之外的另外兩個方法。 http://www.yiiframework.com/doc-2.0/yii-rest-createaction.html

您可以使用這兩個函數並將它們聲明爲與prepareDataProvider類似,以執行發送電子郵件等更多操作。我自己沒有嘗試過,但我認爲這應該是一條路。

相關問題