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); }
Marwelln Oct 29 '14 at 12:57 on 2014-10-29 12:57
source share