Laravel 5 - How to create custom error page with example

By Hardik Savani | May 12, 2016 | | 7804 Viewer | Category : Laravel


Share this post:

If you think how can i create custom error page like 403, 404, 405, 408, 500, 502, 504 on my laravel application then you can make easily and implement it very easily. You can set custom error page globally in laravel project. if you want to create 404 your custom design error page then you can do it easily.

In this post i am going to show you how to create custom layout for error page, if you think i want to display 404 page layout difference, 505 error page layout difference, 500 error page layout difference for etc. you can do it simple. you have to just create every page have its own blade layout like for 404.blade.php, 404.blade.php 504.blade.php etc.

Ok, so first open app/Exceptions/Handler.php file and just bellow code. Handler.php file is handle your all error like 404, 500 etc, that way i added code if you have blade layout file for special error like 404.blade.php file then it will display your file. So first open handle.php and put bellow code.

app/Exceptions/Handler.php

namespace App\Exceptions;


use Exception;

use Illuminate\Validation\ValidationException;

use Illuminate\Auth\Access\AuthorizationException;

use Illuminate\Database\Eloquent\ModelNotFoundException;

use Symfony\Component\HttpKernel\Exception\HttpException;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;


class Handler extends ExceptionHandler

{


/**

* A list of the exception types that should not be reported.

*

* @var array

*/

protected $dontReport = [

AuthorizationException::class,

HttpException::class,

ModelNotFoundException::class,

ValidationException::class,

];


/**

* Report or log an exception.

*

* This is a great spot to send exceptions to Sentry, Bugsnag, etc.

*

* @param \Exception $e

* @return void

*/

public function report(Exception $e)

{

parent::report($e);

}


/**

* Render an exception into an HTTP response.

*

* @param \Illuminate\Http\Request $request

* @param \Exception $e

* @return \Illuminate\Http\Response

*/

public function render($request, Exception $e)

{

if($this->isHttpException($e)){

if (view()->exists('errors.'.$e->getStatusCode()))

{

return response()->view('errors.'.$e->getStatusCode(), [], $e->getStatusCode());

}

}

return parent::render($request, $e);

}

}

Now we are ready to create custom blade layout file for our specific error. In this example i going to create 404.blade.php file for only 404 error code. if you want to create more like then you can create like this way for 403.blade.php, 405.blade.php etc. So first create 404.blade.php file on errors folder and put bellow code.

resources/views/errors/404.blade.php

@extends('layouts.app')


@section('content')

<div class="error-page">

<h2 class="headline text-info"> 404</h2>

<div class="error-content">

<h3><i class="fa fa-warning text-yellow"></i> Oops! Page not found.</h3>

<p>

We could not find the page you were looking for.

Meanwhile, you may <a href="">return to dashboard</a> or try using the search form.

</p>

</div>

</div>

@endsection

You can do it. It is pertty good....



We are Recommending you: