2016-02-25 103 views
1

我问这个问题,因为我没有在这个问题404错误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错误?

回答

7

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

0

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

我只是改变这一行应用程序/例外/ 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)) 
      {