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

notfoundhttpexception in routecollection php line 161

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

In this post we will give you information about [Solved] Laravel Error : NotFoundHttpException in RouteCollection.php Example. Hear we will give you detail about [Solved] Laravel Error : NotFoundHttpException in RouteCollection.php ExampleAnd how to use it also give you demo for it if it is necessary.

 

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 :AppExceptionsHandler.php

  1. public functionrender($request, Exception $e)
  2. {
  3. if($e instanceof SymfonyComponentHttpKernelExceptionNotFoundHttpException){
  4.          // Your stuff here
  5. returnresponse()->view(‘errors.’.$e->getStatusCode(),[],$e->getStatusCode());
  6. }
  7. return parent::render($request,$e);
  8. }

    public function render($request, Exception $e)
	    {
	        if ($e instanceof SymfonyComponentHttpKernelExceptionNotFoundHttpException) {
	       		  // 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.

 

It’s very important to handle exception so that you can control the execution flow of the application.

See also :  php get_headers

you can click here to know more about custom error handler in Laravel : Laravel 5 Create 404 Page Not Found HTTP Custom Error Exception Handler

Hope this code and post will helped you for implement [Solved] Laravel Error : NotFoundHttpException in RouteCollection.php Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us.

Leave a Comment