Laravel 4 使用 App::error 和 App::abort(404)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25060801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:53:08  来源:igfitidea点击:

Laravel 4 using App::error with App::abort(404)

phprestlaravellaravel-4

提问by user3245295

I am trying to write a RESTful API using Laravel.

我正在尝试使用 Laravel 编写一个 RESTful API。

My route is so:

我的路线是这样的:

Route::resource('user', 'UserController');

A method is so:

一种方法是这样的:

public function show($id)
{
    return User::findOrFail($id);
}

But if the model instance doesn't exist I get a 500 error, whereas I believe I need a 404 error.

但是如果模型实例不存在,我会收到 500 错误,而我相信我需要 404 错误。

So in start/global.php I have this:

所以在 start/global.php 我有这个:

App::error(function(Illuminate\Database\Eloquent\ModelNotFoundException $exception)
{
    App::abort(404);
});

Which gives me a 200 code and the message Error in exception handler.

这给了我一个 200 代码和异常处理程序中的错误消息。

Am I doing something wrong? I expect no output and a 404 code.

难道我做错了什么?我希望没有输出和 404 代码。

Thanks,

谢谢,

Michael

迈克尔

回答by lowerends

Just for completeness, the behaviour that the OP mentions (and that can be reproduced), was discussed before on Laravel's Github: https://github.com/laravel/framework/issues/1807. It was decided that it is as designed and will not be changed.

为了完整起见,之前在 Laravel 的 Github 上讨论了 OP 提到的行为(并且可以复制):https: //github.com/laravel/framework/issues/1807。决定它是按设计的,不会改变。

Therefore, returning Response::make("Not Found", 404);or Response::json(null, 404);for JSON from within the error handler is the only solution at this moment, as already mentioned by Michael Bearjaws.

因此,正如Michael Bearjaws已经提到的,从错误处理程序中返回Response::make("Not Found", 404);Response::json(null, 404);JSON 是目前唯一的解决方案。

回答by Michael Bearjaws

You will want to use Response::make("Not Found", 404);to actually return a 404 header and display the "Not Found"message.

您将希望使用Response::make("Not Found", 404);实际返回 404 标头并显示"Not Found"消息。

You can also use a view to render your own custom 404 display with the following

您还可以使用视图来呈现您自己的自定义 404 显示,如下所示

return Response::view('missing', array(), 404);

return Response::view('missing', array(), 404);

You may also add a Missing handler to your application so that it will work with your code

您还可以向您的应用程序添加一个 Missing 处理程序,以便它可以与您的代码一起使用

App::missing(function($exception) { 
    return Response::view('missing', array(), 404); 
}); 

回答by Ben

Change the debugconfig value in app/config/local/app.php(if you're on local, as you should) to falseto assert you're actually getting a 404.

debug配置值更改为app/config/local/app.php(如果您在本地,则应该如此)false以断言您实际上获得了 404。

You could also:

你也可以:

  • Run a unit test with the phpdoc: @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException. recommended
  • Push your commit to development to see if it's actually working.
  • 使用 phpdoc: 运行单元测试@expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException受到推崇的
  • 推动你对开发的承诺,看看它是否真的有效。