2016-02-25 102 views
1

我正在使用Yii2在REST API上工作,我試圖自定義error response。默認情況下,如果我提交請求時使用了錯誤的憑據我看到這一點:Yii2 Rest Api自定義錯誤響應

{ 
    "name": "Unauthorized", 
    "message": "You are requesting with an invalid credential.", 
    "code": 0, 
    "status": 401 
} 

在哪裏,我怎麼能刪除codename線?

回答

2

在您的應用程序配置中嘗試此代碼。

return [ 
    // ... 
    'components' => [ 
     'response' => [ 
      'class' => 'yii\web\Response', 
      'on beforeSend' => function ($event) { 
       $response = $event->sender; 
       if ($response->data !== null && Yii::$app->request->get('suppress_response_code')) { 
        $response->data = [ 
         'success' => $response->isSuccessful, 
         'data' => $response->data, 
        ]; 
        $response->statusCode = 200; 
       } 
      }, 
     ], 
    ], 
]; 

更多Details

+1

謝謝。正是我在找的東西。 – Littlebob