yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.9k forks source link

RESTFul About 404 error handling #14775

Open xutl opened 7 years ago

xutl commented 7 years ago

What steps will reproduce the problem?

  1. open restful url http://localhost/api/users/1
    return 200 ok
  2. open restful url http://localhost/api/users/111111111
    return 404 this is normal
  3. When opening a non-existent URL e.g http://localhost/api/abcdefg Returned the html 404 page,He should return the same format as xml or json as in the second step.

What is the expected result?

Returns a formatted xml or json.

What do you get instead?

Additional info

Q A
Yii version 2.0.12
PHP version 7.1.6
Operating system centos 7.1
schmunk42 commented 7 years ago

Do you have an ApiController or a UsersController in an api module? If the latter your app might not know about the fact that you wanted an API page, when you're serving mixed content.

samdark commented 7 years ago

As @schmunk42 said, in order to respond 404 in this case, custom error handler is needed. I think it's a good idea to document it. Pretty standard use case.

dmirogin commented 7 years ago

This usecase has not documented yet.

pgyf commented 7 years ago

it is handled in this action

        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
sunjayaali commented 7 years ago

@phpyii have you try in rest application? @samdark so you mean custom error handler is not available until 2.0.16?

cebe commented 7 years ago

custom error handler does already work, the only thing that is missing in this issue is documentation.

jurchiks commented 6 years ago

I've tried all kinds of "solutions" for this problem and none of them work for me. My current setup:

All existing actions work perfectly fine. However, when I make a request to a non-existing action in this module (e.g. /api/index/no-such-action), I get the default site/error HTML page! It even ignores the Accept header, which is set to application/json. Module#init() is not even called, thus anything I try to attempt to do there or in the controller is useless.

How am I supposed to work around this? I don't see a non-hackish way to fix this.

yii-bot commented 6 years ago

Issue moved to https://github.com/yiisoft/yii2-rest/issues/13