2017-06-26 11:52:05 2954次浏览 2条回答 0 悬赏 10 金钱

我开启了restfunll API 数据返回和验证都没有问题了,
config里也配置了

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

但是就是访问的路径不存在的时候,返回的还是html
因为配置里

'errorHandler' => [
    'errorAction' => 'site/error',
],

所以会去找site/error
我在 SiteController.php中写了

public function actionError(){

        return ['status'=>'fail','msg'=>'load 错误'];
        if($error=Yii::app()->errorHandler->error)
        {
            return $error;
        }
    }

提示找不到error.php view
目前金钱 只有 5

补充于 2017-06-26 12:12

QQ图片20170626121159.png

补充于 2017-06-26 12:18

不太明白你的意思,我去看了一下,getIsSuccessful是response的一个方法,返回的如果$_statusCode 的值在200-300之间为真,$_statusCode的默认值为200,不知道什么时候会改变这个值

补充于 2017-06-26 14:49

问题总结:
RESTFull web server's error handing 里说的如果想要自定义错误响应,需要在config里添加:

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

实践证明,除了给reponse添加class,on beforeSend,还需要指定format为FORMAT_JSON
具体代码如下:

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

本问题得到解答,感谢
@ymfhack

最佳答案

  • ymfhack 发布于 2017-06-26 11:58 举报
    /**
         * @return bool whether this response is successful
         */
        public function getIsSuccessful()
        {
            return $this->getStatusCode() >= 200 && $this->getStatusCode() < 300;
        }
    
    2 条回复
    回复于 2017-06-26 12:21 回复

    不太明白你的意思,我去看了一下,getIsSuccessful是response的一个方法,返回的如果$_statusCode 的值在200-300之间为真,$_statusCode的默认值为200,不知道什么时候会改变这个值

    回复于 2017-06-26 12:30 回复

    谢谢您,真心感谢,我实现了,config里边还需要添加 format为json

    'response' => [
                'class' => 'yii\web\Response',
                'format' => yii\web\Response::FORMAT_JSON,
                'charset' => 'UTF-8',
                'on beforeSend' => function ($event) {
                    $response = $event->sender;
                    if ($response->data !== null && !empty(Yii::$app->request->get('suppress_response_code'))) {
                        $response->data = [
                            'success' => $response->isSuccessful,
                            'data' => $response->data,
                        ];
                        $response->statusCode = 200;
                    }
                },
            ],
    
您需要登录后才可以回答。登录 | 立即注册
陈诺
主管

陈诺 北京

注册时间:2017-06-26
最后登录:2018-03-20
在线时长:5小时51分
  • 粉丝4
  • 金钱610
  • 威望0
  • 积分660

热门问题