如何为 ModelNotFoundException laravel 显示 404

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25452420/
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:58:55  来源:igfitidea点击:

How to display 404 for ModelNotFoundException laravel

phplaravel

提问by davidxd33

I'm trying to set up a smart error catching system in Laravel so I can define a view for every error instead of the traditional "Something went wrong here." message with debugging off. I've successfully caught typical http headers such as 404, 403, 405, and 500 however now I would like the catch the ModelNotFoundException and simply return 404. Here's what I've got so far:

我正在尝试在 Laravel 中设置一个智能错误捕获系统,以便我可以为每个错误定义一个视图,而不是传统的“这里出了点问题”。调试关闭的消息。我已经成功捕获了典型的 http 标头,例如 404、403、405 和 500,但是现在我想要捕获 ModelNotFoundException 并简单地返回 404。这是我到目前为止所得到的:

App::error(function(Exception $e, $code) {

  $error = ['code' => $code];

  switch($code) {

    case 403:

      return Response::view('errors.error', $error, $code);

    break;

    case 404:

      return Response::view('errors.error', $error, $code);

    break;

    case 405:

      return Response::view('errors.error', $error, $code);

    break;

    case 500:

      return Response::view('errors.error', $error, $code);

    break;

    default:

      return;

    break;

  }

    /*  
      | Returns null if the error is not 401, 403, 404, 500, 405
      | I just have a default of null in the switch on $code.

      if(!in_array($code, [401, 403, 404, 500, 405])) {

        return;  

      }

    */


});


App::error(function(ModelNotFoundException $e) {

  return Response::view('errors.error', ['code' => 404], 404);

});

Now detecting the 403, 404, 405, 500 headers works perfect however when I added another App::error to listen for ModelNotFoundException's, all fails. For every exception I am returned null. By default, a 500 header is thrown by ModelNotFoundException and that is what is displayed in my view. I want it to be a 404.

现在检测 403、404、405、500 标头工作完美,但是当我添加另一个 App::error 来侦听 ModelNotFoundException 时,一切都失败了。对于每个异常,我都返回 null。默认情况下,ModelNotFoundException 会抛出一个 500 标头,这就是我视图中显示的内容。我希望它是 404。

TL;DR

TL; 博士

  • I would like ModelNotFoundException to throw 404 instead of 500.
  • 我希望 ModelNotFoundException 抛出 404 而不是 500。

回答by Samuel De Backer

First you need a 404.blade.phpfile in views/errorsdirectory.
Here is how to throw a 404 error when a ModelNotFoundException occurs:

首先,您需要在views/errors目录中有一个404.blade.php文件。 以下是发生 ModelNotFoundException 时如何抛出 404 错误:

Laravel 4
In app/start/global.php

Laravel 4
app/start/global.php

App::error(function(Illuminate\Database\Eloquent\ModelNotFoundException $exception, $code)
{
    return Response::view('errors.404', array(), 404);
});

Laravel 5.0 & 5.1
Add this code to render()method in /app/exceptions/handler.php:

Laravel 5.0 & 5.1
将此代码添加到/app/exceptions/handler.php 中的render()方法:

if ($e instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
    return response()->view('errors.404', array(), 404);
}

Laravel 5.2+
A 404 error is automatically returned when a ModelNotFound exception occurs.

Laravel 5.2+
出现 ModelNotFound 异常时自动返回 404 错误。

回答by davidxd33

Resolved--the issue was namespacing. The correct namespace for the exception was:

已解决 - 问题是命名空间。异常的正确命名空间是:

\Illuminate\Database\Eloquent\ModelNotFoundException