In this post, i will let you know how to handle Not Found HTTP Exception in Laravel.
This is very common error when you do any mistake in your routing. So as a PHP Developer you should handle all the exception in your running application.
Question is when do you need to use exception and why ?
Exception is an event that occur during the execution of program and disrupt the normal flow of instructions.
In Laravel, there are handler class through which you can handle all the exception in your application.
There are two main methods in Handler class :
report()
method is used to log exceptions and render()
method is used to convert exception in HTTP response.
You will find the Handler class in following path :App\Exceptions\Handler.php
- public function render($request, Exception $e)
- {
- if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
- // Your stuff here
- return response()->view('errors.'.$e->getStatusCode(), [], $e->getStatusCode());
- }
- return parent::render($request, $e);
- }
Now create a 404.blade.php
file in following path resources/views/errors
to display your own custom message for Not Found HTTP Exception.