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.
- public functionrender($request, Exception $e)
- {
- if($this->isHttpException($e)){
- if(view()->exists('errors.'.$e->getStatusCode()))
- {
- returnresponse()->view('errors.'.$e->getStatusCode(),[],$e->getStatusCode());
- }else{
- returnresponse()->view('errors.custom',[],$e->getStatusCode());
- }
- }
- return parent::render($request,$e);
- }
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.
- if($request->ajax()){
- returnresponse()->json(['error'=>'Not Found'],404);
- }
if ($request->ajax()) { return response()->json(['error' => 'Not Found'], 404); }You can use switch case too for specific HTTP Status error code.
- if($this->isHttpException($e))
- {
- switch($e->getStatusCode()){
- // not found
- case404:
- returnresponse()->view('errors.404',[],404);
- break;
- // internal server error
- case'500':
- returnresponse()->view('errors.500',[],500);
- break;
- default:
- return$this->renderHttpException($e);
- break;
- }
- }
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
- <divclass="page_content_wrap">
- <divclass="content_wrap">
- <divclass="text-align-center error-404">
- <h1class="huge">404</h1>
- <hrclass="sm">
- <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>
<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>
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