Laravel 5.2.15中的404错误处理

时间:2016-02-25 17:43:45

标签: php laravel-5.1 laravel-5.2

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

laravel routing and 404 error

在上面的答案中,我们可以看到下面的代码将在filters.php

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

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

2 个答案:

答案 0 :(得分:9)

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

答案 1 :(得分:0)

看一看 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))
            {