Laravel 4始终返回HTTP状态代码200 [英] Laravel 4 always returns HTTP status code 200
问题描述
我在error.php中有以下代码,该代码是使用控制器中的App::abort(404, $error)
触发的.我的响应状态代码仍然是200(确定).我尝试了各种错误代码,例如400、403
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);
});
推荐答案
对于仍在使用此问题的人:
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.
检查所有控制器,并确保<?php
标记前面没有空格. < ?php
标记应该是文件中的第一件事.在任何这样路由的控制器中,<?php
标记前面的单个空格:
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');
将导致所有状态代码均为200.
Will cause all status codes to be 200.
这篇关于Laravel 4始终返回HTTP状态代码200的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!