2016-05-17 165 views
1

我在yii2中使用休息API Authorization : Bearer和我的update操作需要使用PUT發送數據。我已經完全配置了actionUpdate,但不知何故,我在Request PUT中沒有收到任何數據。Yii2請求PUT無法正常工作

我在網上發現了幾篇關於Yii2 PUT問題的文章,但是找不到天氣有沒有解決方案呢?

一個文章或發行的是github issue並將其指向這個github issue

廣告,如果沒有辦法解決比我應該用什麼替代的Update行動呢。

這裏是我的actionUpdate代碼

public function actionUpdate($id) 
{ 
    $params = Yii::$app->request->bodyParams; 

    $model = Event::find()->where(['event_id'=>$id])->andWhere(['partner_id' => Yii::$app->user->id])->one(); 

    if($model !== null){ 

     $model->load($params, ''); 
     $model->partner_id = Yii::$app->user->id; 
     $model->updated_date = time(); 

     if ($model->save()) { 

      $this->setHeader(200); 
      echo json_encode(array('status'=>1,'data'=>array_filter($model->attributes)),JSON_PRETTY_PRINT); 

     } 
    } 
} 

這是調試屏幕的截圖。請參閱event_name屬性。

enter image description here

這是$model->load($params,'')線的執行之後的屏幕截圖。

我打電話的服務如下,不能Update正確的數據。我的服務通過郵遞員工作正常。所以我想我在CURL請求中缺少一些東西。

$service_url = 'http://localhost/site-api/api/web/v1/events/'.$eventDetailDBI->gv ('id'); 
      $curl = curl_init($service_url); 
      $curl_post_data = array(
       "event_name" => $eventDetailDBI->gv ('name'), 

      ); 

      $header = array(); 
      $header[] = 'Authorization: Bearer 4p9mj82PTl1BWSya7bfpU_Nm'; 
      $header[] = 'Content-Type: application/x-www-form-urlencoded'; 

      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
      curl_setopt($curl, CURLOPT_HTTPHEADER,$header); 
      curl_setopt($curl, CURLOPT_PUT, 1); 
      curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data); 
      $curl_response = curl_exec($curl); 
      $json = json_decode($curl_response, true); 
      curl_close($curl); 

我在我POST領域得到正確的數據,傳遞正確的數據,但該服務犯規更新任何數據。

謝謝

+0

請郵寄樣本代碼。謝謝 –

+0

哪些問題?我不知道他們的github回購中有任何開放的PUT相關問題。我沒有得到PUT工作的唯一時間是因爲我的IIS服務器默認情況下禁用了PUT動詞,並且與Yii無關。你能否通過分享代碼或鏈接任何這些文章來指定PUT問題? –

回答

1

試試這個:

public function actionUpdate($id) 
{ 
    // this will get what you did send as application/x-www-form-urlencoded params 
    // note that if you are sending data as query params you can use Yii::$app->request->queryParams instead. 
    $params = Yii::$app->request->bodyParams; 

    $model = Event::find()->where(['event_id'=>$id])->andWhere(['partner_id' => Yii::$app->user->id])->one(); 

    if($model !== null){ 

     // This will load data to your safe attribute as defined in your model rules using your default scenario. 
     $model->load($params, ''); 

     $model->partner_id = Yii::$app->user->id; 
     $model->updated_date = time(); 

     if ($model->save()) { 

      /* 
       you can use Yii::$app->getResponse()->setStatusCode(200) here but no need to do that. 
       response will be 200 by default as you are returning data. 
      */ 

      // yii\rest\Serializer will take care here of encoding model's related attributes. 
      return [ 
       'status' => 1, 
       'data' => $model 
      ]; 

     } 
     else { 
      // when validation fails. you model instance will hold error messages and response will be auto set to 422. 
      return $model; 
     } 
    } 
} 
+0

它的工作方式。我確實看到了'$ params'中的參數,但它也包含許多其他數據,所以我不知道如何只在它中加載模型屬性。 '$ params'具有其他值,如'WebKitFormBoundary'等 –

+0

那是什麼[load()](http://www.yiiframework.com/doc-2.0/yii-base-model.html#load() - 詳情)。它只會從params得到你的model :: rules()認爲是安全的屬性。換句話說,如果'WebKitFormBoundary'沒有在你的模型規則()中定義,或者至少沒有設置爲安全的,那麼在將數據加載到你的模型時它將被忽略。有關更多詳細信息,請參閱[Yii文檔](http://www.yiiframework.com/doc-2.0/guide-structure-models.html#validation-rules)。 –

+0

我從你的評論上面那行代碼中得到了。但它沒有加載數據。我正在上傳調試截圖。我現在正在向郵遞員提出要求。我知道這不重要,但只要你知道它是否會影響。 –