B
B
Bags2017-04-20 13:10:57
Yii
Bags, 2017-04-20 13:10:57

How to properly configure error output in Yii2 api?

I am writing a site api on a separate domain api.site.ru I
use the basic template, controllers and api models are placed in the modules/v1/ folder
For example, when requesting a non-existent page api.site.ru/asdqwe, the server gives a 404 error, but along with html code, even through the console.
How to override and display errors without a template and html code?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-04-20
@webinar

Apparently you need to configure response. If everywhere, then through the config, if in a specific action, then you can do this:

Yii::$app->response->format = Response::FORMAT_JSON;
Yii::$app->response->setStatusCode(404,'Вы запросили не существующую хрень'));
return false;

And about globally in the config - this is described in the docs in the RESTful Web Service section:
www.yiiframework.com/doc-2.0/guide-rest-quick-star...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question