[Solved] Laravel Error : NotFoundHttpException in RouteCollection.php Example

[Solved] Laravel Error : NotFoundHttpException in RouteCollection.php Example

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

  1. public function render($request, Exception $e)
  2. {
  3. if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
  4.          // Your stuff here
  5. return response()->view('errors.'.$e->getStatusCode(), [], $e->getStatusCode());
  6. }
  7. return parent::render($request, $e);
  8. }

Now create a 404.blade.php file in following path resources/views/errors to display your own custom message for Not Found HTTP Exception.

Phone: (+91) 8800417876
Noida, 201301