Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos

Laravel Custom 404 Error Page Example

Posted on October 30, 2019December 17, 2022 By XpertPhp No Comments on Laravel Custom 404 Error Page Example

Today, In this tutorial we explain to you how to create custom create 404 and 500 error page in laravel(Laravel Custom 404 Error Page Example). why we need to this error page? when visitor wrong type web URL use in our site and that type of page not available in our site then we can display an error page.

We can easy to makes custom error message using the exception handler. laravel provides exception handler class and it always checks all exceptions thrown. so it’s depending on the exception.

There are two methods available in handle class. such as report and render. when the report method handles the current request and returns the without rendering an error page. the render method gives responds with an error message. it’s converting HTTP response status from the exception and even sends it back to the browser.

Modify The Handler.php File

Let’s create a custom 404 page so we have to modify the render method in app/Exceptions/Handler.php file.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];
    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];
    /**
     * Report or log an exception.
     *
     * @param  \Exception  $exception
     * @return void
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }
    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        if($this->isHttpException($exception)){
            switch ($exception->getStatusCode()) {
                case '404':
                     return $this->renderHttpException($exception);
                break;
                case '500':
return $this->renderHttpException($exception);  
                break;
                default:
                    return $this->renderHttpException($exception);
                break;
            }
        }else{
            return parent::render($request, $exception);
        }
    }
}
?>
See also  Laravel 6 Image Upload tutorial example

Create Blade Files

So finally, first we will create the new directory “resources/views/errors” and that directory in create a “resources/views/errors/404.blade.php” file. paste the below code in this file.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<html>
<head>
<style>
#notfound{position:relative;height:100vh}#notfound .notfound{position:absolute;left:50%;top:50%;-webkit-transform:translate(-50%,-50%);-ms-transform:translate(-50%,-50%);transform:translate(-50%,-50%)}.notfound .notfound-404{position:relative;height:150px;line-height:150px;margin-bottom:25px}.notfound .notfound-404 h1{font-family:titillium web,sans-serif;font-size:186px;font-weight:900;margin:0;text-transform:uppercase;-webkit-background-clip:text;background-size:cover;background-position:center}.notfound h2{font-family:titillium web,sans-serif;font-size:26px;font-weight:700;margin:0}.notfound p{font-family:montserrat,sans-serif;font-size:14px;font-weight:500;margin-bottom:0;text-transform:uppercase}.notfound a{font-family:titillium web,sans-serif;display:inline-block;text-transform:uppercase;color:#fff;text-decoration:none;border:none;background:#5c91fe;padding:10px 40px;font-size:14px;font-weight:700;border-radius:1px;margin-top:15px;-webkit-transition:.2s all;transition:.2s all}
</style>
</head>
<body>
<div id="notfound">
<div class="notfound">
<div class="notfound-404">
  <h1>404</h1>
</div>
<h2>Oops! This Page Could Not Be Found</h2>
<p>Sorry but the page you are looking for does not exist, have been removed. name changed or is temporarily unavailable</p>
<a href="#">Go To Homepage</a>
</div>
</div>
</body>
</html>

Laravel Tags:laravel 6

Post navigation

Previous Post: Laravel hasOneThrough Eloquent Relationship Tutorial Example
Next Post: Laravel 6 Image Upload tutorial example

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com