Laravel – How to create custom error page with example

Laravel – How to create custom error page with example

In this post we will give you information about Laravel – How to create custom error page with example. Hear we will give you detail about Laravel – How to create custom error page with exampleAnd how to use it also give you demo for it if it is necessary.

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 AppExceptions;


use Exception;

use IlluminateValidationValidationException;

use IlluminateAuthAccessAuthorizationException;

use IlluminateDatabaseEloquentModelNotFoundException;

use SymfonyComponentHttpKernelExceptionHttpException;

use IlluminateFoundationExceptionsHandler 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 IlluminateHttpRequest $request

* @param Exception $e

* @return IlluminateHttpResponse

*/

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

Also see:Laravel Create Custom Artisan Command with Example

@extends('layouts.app')


@section('content')

<div >

<h2 > 404</h2>

<div >

<h3><i ></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….

Hope this code and post will helped you for implement Laravel – How to create custom error page with 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. 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 *

7  +  1  =  

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