可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have below code in error.php, which is triggered using App::abort(404, $error)
in my controller. Still my response status code is 200(ok). I tried with various error codes like 400, 403
// NotFoundException handler App::error(function(NotFoundException $e) { $default_message = 'The requested resource was not found'; return Response::json(array( 'error' => $e->getMessage() ?: $default_message, ), 404); });
回答1:
For anyone still googling this problem:
I was struggling with this problem for hours. For me the problem was caused by an issue with one of my controllers.
Check all of your controllers and make sure there are no spaces in front of the <?php
tag. The <?php
tag should be the very first thing in the file. A single space in front of the <?php
tag in any of your controllers that are routed as such:
Route::controller('example', 'ExampleController');
Will cause all status codes to be 200.
回答2:
I believe, regardless, you should receive a 404 response, so there might be something else happening that's the result of code not included in your question.
That being said, the Exception class that is thrown for 404 is NotFoundHttpException
rather than NotFoundException
.
Since Laravel 4 uses Symfony's HttpKernal, that Exception is here.
You can see here where App::abort()
throws NotFoundHttpException
when a 404 is triggered.
Therefore, your code should look like:
// NotFoundHttpException handler App::error(function(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e) { $default_message = 'The requested resource was not found'; return Response::json(array( 'error' => $e->getMessage() ?: $default_message, ), 404); });
Important: This will only fire for a 404 status, as that's the corresponding code to NotFoundHttpException. Other status codes return other Exception classes. To capture all HTTP status error codes exceptions, type hint for HttpException like so:
// HttpException handler App::error(function(\Symfony\Component\HttpKernel\Exception\HttpException $e) { return Response::json(array( 'error' => $e->getMessage(), ), $e-> getStatusCode()); });
Lastly, consider using a bit of Content Negotiation when deciding to return JSON or HTML.
回答3:
The solution didn't worked for me, so in case anyone is still looking for an answer, I thought it be best to put it here instead of creating another question.
After some time I had this problem too, in my app/Exceptions/Handler.php
I had:
if ($e instanceof ModelNotFoundException) { if ($request->ajax()) { return response() ->json(['error' => ['No results']]) ->header('status', 422); } }
This worked in my local environment, however, in the homolog environment (which reproduces the production environment, just to be clear) it didn't returned the correct status code.
After another look I started looking at Laravel's docs, and I changed the call to the following:
return response() ->json(['error' => ['No results.']], 422);
And that did the trick. Hope this can help.