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

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

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.php 403.blade.php 404.blade.php 500.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 function render($request, Exception $e)
  2. {
  3. if($this->isHttpException($e)){
  4. if (view()->exists('errors.'.$e->getStatusCode()))
  5. {
  6. return response()->view('errors.'.$e->getStatusCode(), [], $e->getStatusCode());
  7. }else{
  8. return response()->view('errors.custom', [], $e->getStatusCode());
  9. }
  10. }
  11. return parent::render($request, $e);
  12. }

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

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

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.         case 404:
  6.                     return response()->view('errors.404',[],404);
  7.         break;
  8.         // internal server error
  9.         case '500':
  10.                  return response()->view('errors.500',[],500);    
  11.         break;
  12.         default:
  13.              return $this->renderHttpException($e);
  14.         break;
  15.     }
  16. }
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. <div class="page_content_wrap">
  2. <div class="content_wrap">
  3.         <div class="text-align-center error-404">
  4. <h1 class="huge">404</h1>
  5. <hr class="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>

Phone: (+91) 8800417876
Noida, 201301
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
porno izle
sohbet okey oyna
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat

viagra fiyatı

viagra 100 mg fiyat

viagra sipariş

orjinal viagra

viagra satın al

izmir escort