Yii2 and processing exceptions

I am creating a REST APi for my Yii2 based application . So, I have a problem with exception handling. For example, I need to throw out HTTP 405 code when someone uses the wrong HTTP verb , but I want to send something like this:

{meta:{error:{code:405,message:"Wrong method"}}}

So, I need to catch the Exception and modify the Response . But how can I do this? Yii had onError and onException events . What about Yii2?

+4
source share
1 answer

:

    'response' => [
        'format' => yii\web\Response::FORMAT_JSON,
        'charset' => 'UTF-8'
    ]

- :

throw new \yii\web\HttpException(400, 'Wrong method', 405);
+4

Source: https://habr.com/ru/post/1540084/


All Articles