Laravel 5.2.15 中的 404 错误处理

404 Error Handling in Laravel 5.2.15

我问这个问题是因为我在这个问题中添加评论后没有得到回复

laravel routing and 404 error

在上面的回答中,我们可以看到下面的代码用于filters.php

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

但是,我想我们在最新版本中没有 filters.php。有人可以建议更好的方法来处理 404 错误吗?

你不需要再这样做了。不要包括那个。您所做的是在您的 resources/views/errors 文件夹中放置一个名为 404.blade.php 的视图文件(您的 404 错误视图),然后 Laravel 将为您处理 404 错误。

看看 http://www.jeffmould.com/2016/05/25/laravel-5-error-handling/

我只是更改了这一行 App/Exceptions/Handler.php 文件。

public function render($request, Exception $e)
    {
        // the below code is for Whoops support. Since Whoops can open some security holes we want to only have it
        // enabled in the debug environment. We also don't want Whoops to handle 404 and Validation related exceptions.
        if (config('app.debug') && !($e instanceof ValidationException) && !($e instanceof HttpResponseException))
        {

 /******************here I changed**********************/


           # return $this->renderExceptionWithWhoops($e);
           return response()->view('errors.404', [], 404);
        }


        // this line allows you to redirect to a route or even back to the current page if there is a CSRF Token Mismatch
        if($e instanceof TokenMismatchException){
            return redirect()->route('index');
        }       

        // let's add some support if a Model is not found 
        // for example, if you were to run a query for User #10000 and that user didn't exist we can return a 404 error
        if ($e instanceof ModelNotFoundException) {
            return response()->view('errors.404', [], 404);
        }  

        // Let's return a default error page instead of the ugly Laravel error page when we have fatal exceptions
        if($e instanceof \Symfony\Component\Debug\Exception\FatalErrorException) {
            return \Response::view('errors.500',array(),500);
        }

        // finally we are back to the original default error handling provided by Laravel
        if($this->isHttpException($e))
        {
            switch ($e->getStatusCode()) {
                // not found
                case 404:
                    return \Response::view('errors.404',array(),404);
                break;
                // internal error
                case 500:
                    return \Response::view('errors.500',array(),500);   
                break;

                default:
                    return $this->renderHttpException($e);
                break;
            }
        }
        else
        {
            return parent::render($request, $e);
        }      
 /******************here I changed**********************/

        #return parent::render($request, $e);
    }

     if (config('app.debug') && !($e instanceof ValidationException) && !($e instanceof HttpResponseException))
            {