Yii2 rest api没有返回响应代码和状态

时间:2015-08-11 07:15:11

标签: php rest yii yii2 yii2-advanced-app

我正在尝试获取(StatusCode)以响应REST api,而它只是返回字段名称和错误消息,如此

[{"field":"Email","message":"Email \"ali@ali.ali\" has already been taken."}]

我已添加回复

'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;
            }
        },

    ],

2 个答案:

答案 0 :(得分:5)

尝试这种方式,它对我有用:

class C {
public:
    C(string&& s) : m_s{s} {} 
private:
    string& m_s;
};

答案 1 :(得分:0)

Yii::$app->response->statusCode

您可以在返回回复之前添加此代码