Laravel 5 Create 404 Page Not Found HTTP Custom Error Exception Handler

Laravel 5 Create 404 Page Not Found HTTP Custom Error Exception Handler

In this post we will give you information about Laravel 5 Create 404 Page Not Found HTTP Custom Error Exception Handler. Hear we will give you detail about Laravel 5 Create 404 Page Not Found HTTP Custom Error Exception HandlerAnd how to use it also give you demo for it if it is necessary.

Laravel 5 Create a 404 Page Not Found HTTP Custom Error Exception Handler

Some Exception in PHP tell the HTTP error code generated from the server. For Example ‘Page Not Found’ have 404 error code and sometime it generated 500 error code which tell the server error.

In Laravel you can create your own custom error pages based on different HTTP status codes.

Create all pages releted to HTTP status code in following path resources/views/errors/

Here a common list of HTTP status code which you face everytime in application :

  • 400 for Bad Request
  • 403 for Forbidden
  • 404 for Not Found
  • 408 for Request Timeout
  • 408 for Request Timeout
  • 500 for Internal Server Error

You can create a view file like 400.blade.php403.blade.php404.blade.php500.blade.php for this type of HTTP status code .

Where to Place Error Handler in Laravel

You can make changes in following path app/Exceptions/Handler.php within render method.

Here we check first if error is http exception then we render custom error pages.

We can check HTTP Exception by $this->isHttpException($e) mthod.

Now just simply made changes in your Handler.php by following line of code.

  1. public functionrender($request, Exception $e)
  2. {
  3. if($this->isHttpException($e)){
  4. if(view()->exists('errors.'.$e->getStatusCode()))
  5. {
  6. returnresponse()->view('errors.'.$e->getStatusCode(),[],$e->getStatusCode());
  7. }else{
  8. returnresponse()->view('errors.custom',[],$e->getStatusCode());
  9. }
  10. }
  11. return parent::render($request,$e);
  12. }
 public function render($request, Exception $e)    {        if($this->isHttpException($e)){            if (view()->exists('errors.'.$e->getStatusCode()))            {                return response()->view('errors.'.$e->getStatusCode(), [], $e->getStatusCode());            }else{            return response()->view('errors.custom', [], $e->getStatusCode());            }        }        return parent::render($request, $e);    }

You can also check if request is ajax then you can return json feedback.

  1. if($request->ajax()){
  2. returnresponse()->json(['error'=>'Not Found'],404);
  3. }
  if ($request->ajax()) {       return response()->json(['error' => 'Not Found'], 404);   }

You can use switch case too for specific HTTP Status error code.

  1. if($this->isHttpException($e))
  2. {
  3.     switch($e->getStatusCode()){
  4.         // not found
  5.         case404:
  6.                     returnresponse()->view('errors.404',[],404);
  7.         break;
  8.         // internal server error
  9.         case'500':
  10.                  returnresponse()->view('errors.500',[],500);    
  11.         break;
  12.         default:
  13.              return$this->renderHttpException($e);
  14.         break;
  15.     }
  16. }
    if($this->isHttpException($e))        {        	switch ($e->getStatusCode()) {        		// not found        		case 404:					return response()->view('errors.404',[],404);        		break;        		// internal server error        		case '500':				    return response()->view('errors.500',[],500);	        		break;        		default:	        		return $this->renderHttpException($e);        		break;        	}        }

Create your custom error page

Now create a page accroding HTTP Status code, here i am creating page 404.blade.php in following path resources/views/errors/404.blade.php

  1. <divclass="page_content_wrap">
  2. <divclass="content_wrap">
  3.         <divclass="text-align-center error-404">
  4. <h1class="huge">404</h1>
  5. <hrclass="sm">
  6. <p><strong>Sorry - Page Not Found!</strong></p>
  7. <p>The page you are looking for was moved, removed, renamed<br>or might never existed. You stumbled upon a broken link :(</p>
  8. </div>
  9.     </div>
  10. </div>
<div >   <div >		<div >          <h1 >404</h1>          <hr >          <p><strong>Sorry - Page Not Found!</strong></p>          <p>The page you are looking for was moved, removed, renamed<br>or might never existed. You stumbled upon a broken link :(</p>        </div>	</div></div>
Now you can manage all pages with HTTP status code manually.

Hope this code and post will helped you for implement Laravel 5 Create 404 Page Not Found HTTP Custom Error Exception Handler. 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. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

79  +    =  87

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US