How to catch exceptions / missing pages in Laravel 5?

In Laravel 5, App::missing and App::error not available, so how are your catch exceptions and missing pages now?

I could not find any information about this in the documentation.

+55
laravel laravel-5
Oct 29 '14 at 12:57 on
source share
8 answers

In Laravel 5, you can catch exceptions by editing the render method in app/Exceptions/Handler.php .

If you want to catch the missing page (also known as NotFoundException ), you need to check if the exception, $e , is an instance of Symfony\Component\HttpKernel\Exception\NotFoundHttpException .

 public function render($request, Exception $e) { if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) return response(view('error.404'), 404); return parent::render($request, $e); } 

With the code above, we check if $e instanceof Symfony\Component\HttpKernel\Exception\NotFoundHttpException , and if we send a response with view file error.404 as content with an HTTP status code 404 .

This can be used for any exception. Therefore, if your application App\Exceptions\MyOwnException exception from App\Exceptions\MyOwnException , you check this instance instead.

 public function render($request, Exception $e) { if ($e instanceof \App\Exceptions\MyOwnException) return ''; // Do something if this exception is thrown here. if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) return response(view('error.404'), 404); return parent::render($request, $e); } 
+86
Oct 29 '14 at 12:57 on
source share

With the commit from today (9db97c3) all you have to do is add the 404.blade.php file to the / resources / views / errors / folder and it will find and display it automatically.

+15
Dec 03 '14 at 21:46
source share

Since it commits 30681dc and 9acf685, the missing() method has been moved to the Illuminate\Exception\Handler class.

So, for some time you could do this:

 app('exception')->missing(function($exception) { return Response::view('errors.missing', [], 404); }); 


... But lately, refactoring was done in 62ae860 .

Now you can add the following to app/Http/Kernel.php :

 // add this... use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Response; class Kernel extends HttpKernel { (...) public function handle($request) { try { return parent::handle($request); } // ... and this: catch (NotFoundHttpException $e) { return Response::view('errors.missing', [], 404); } catch (Exception $e) { throw $e; } } 


Finally, please keep in mind that Laravel is still in a difficult development and changes may happen again.

+4
Nov 03 '14 at 18:23
source share

Angular HTML5 mode may crash the missing pages (custom bookmark multiple pages and try reloading). If you cannot override the server settings to handle this, you might consider ignoring the behavior of skipped pages.

You can change the rendering method \ App \ Exceptions \ Handler to click content from 200, rather than click a 404 template with code 404.

 /** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response */ public function render($request, Exception $e) { if ($this->isHttpException($e) && $e->getStatusCode() == 404) { return response()->view('angular', []); } return parent::render($request, $e); } 
+2
Feb 12
source share

Laravel ships with a default error page, you can easily add error pages, for example,

1 - Create an error view in the view -> error folder
2 - The name must correspond to HTTP errors, for example 404 or 403 500

 `Route::get('/page/not/found',function($closure){ // second param is optional abort(404, 'Page Not found'); });` 
0
Aug 25 '15 at 6:37
source share

Adding the following code

 protected $dontReport = [ 'Symfony\Component\HttpKernel\Exception\HttpException' ]; 

and

 public function render($request, Exception $e) { return redirect('/'); //return parent::render($request, $e); } 

will work right for me

0
May 27 '16 at 12:48
source share

If you want to save the handler in your web routes file after existing routes:

 Route::any( '{all}', function ( $missingRoute) { // $missingRoute } )->where('all', '(.*)'); 
0
Jul 17 '17 at 20:32
source share

One way you can handle this, but not in the best way, is to go to a redirect.

 <?php namespace App\Exceptions; use Exception; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; class Handler extends ExceptionHandler { /** * A list of the exception types that should not be reported. * * @var array */ protected $dontReport = [ 'Symfony\Component\HttpKernel\Exception\HttpException' ]; /** * Report or log an exception. * * This is a great spot to send exceptions to Sentry, Bugsnag, etc. * * @param \Exception $e * @return void */ public function report(Exception $e) { return parent::report($e); } /** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response */ public function render($request, Exception $e) { return redirect('/'); //return parent::render($request, $e); } } 
-2
Mar 22 '15 at 5:12
source share



All Articles