Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lockeyo's avatar

How do i create a custom 404 / error page?

Hello Laravel Community,

i'm new to Laravel and want to ´create a little app with this great framework. Now i have the problem that if some user enters a wrong url => http://www.example.com/abuot (wrong) => http://www.example.com/about (right) i want to present a custom error page. I use Laravel 5, because my app isn't relevant to my business and i want to keep up to the latest technology. How can i create a custom 404 / error page for my web-app? Would be awesome if somebody can help.

Greetings from Germany,

Marcel

0 likes
29 replies
henrique's avatar

I'm not sure about Laravel 5, but for Laravel 4 I use this (app/start/global.php):

App::error(function(Exception $exception, $code)
{
    if (App::isDownForMaintenance()) {
        return Response::make("Maintenance, brb.", 503);
    }

    if (Config::get('app.debug')) return;

    switch ($code)
    {
        case 403: /* permission denied */
        case 404: /* not found */
            return View::make('_layouts.error_404');

        case 500: /* internal error */
        default:
            return View::make('_layouts.error_500');
    }
});
bestmomo's avatar
Level 52

Create a view and set this code in app/Exception/Handler.php :

/**
 * Render an exception into a response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $e
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $e)
{
    if($e instanceof NotFoundHttpException)
    {
        return response()->view('missing', [], 404);
    }
    return parent::render($request, $e);
}

Set this use to get it working :

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
22 likes
muragijimana's avatar

thank you so much to help me after a gig struggle within 3 days!!

Ozan's avatar

Since commits 30681dc and 9acf685 the missing() method had been moved to the Illuminate\Exception\Handler class.

So, for some time, you could do this:

app('exception')->missing(function($exception)
{
    return Response::view('errors.missing', [], 404);
});

... But then recently, a refactoring has been done in 62ae860.

Now, what you can do is adding the following to app/Http/Kernel.php:

// add this...
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Response;

class Kernel extends HttpKernel {

    (...)

    public function handle($request)
    {
        try
        {
            return parent::handle($request);
        }

        // ... and this:
        catch (NotFoundHttpException $e)
        {
            return Response::view('errors.missing', [], 404);
        }

        catch (Exception $e)
        {
            throw $e;
        }
    }
kamlesh's avatar

Hi, I want to show custom error page in my laravel 5 app. for example any user type url like http://www.app.com/url123 (wrong) but http://www.app.com/url (right)

default error show as : Uh-oh, something went wrong! Error Code: 500

but instead I wana show my custom view

how I can do this: same link I prefer but not achieve yet

My current app/Exceptions/Handler.php :

<?php namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use symfony\http-kernel\Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class Handler extends ExceptionHandler {

    protected $dontReport = [
        'Symfony\Component\HttpKernel\Exception\HttpException'
    ];

    public function report(Exception $e)
    {
        return parent::report($e);
    }
    public function render($request, Exception $e)
    {
        if ($this->isHttpException($e))
        {
            return $this->renderHttpException($e);
        }
        else if($e instanceof NotFoundHttpException)
        {
            return response()->view('missing', [], 404);
        }
        else
        {
            return parent::render($request, $e);
        }
    }

}

and I create a error view on : \resources\views\errors\404.blade.php

but still not load my 404.blade.php view

1 like
bestmomo's avatar

@kamlesh dont mind with all this code, now just create a resources\views\errors\404.blade.php and it should work.

3 likes
graham's avatar

I'm not sure if this is just me but I have noticed that Custom 404 doesn't work on my local homestead box. It does work when deployed to a server.

bestmomo's avatar

With all posts on homestead I see on this forum it doesn't make me want to use it :)

graham's avatar

@bestmomo I wonder if the 404 thing is by design.

I'm really impressed with homestead, it's made the local setup so much easier/tidier.

kamlesh's avatar

My current app/Exceptions/Handler.php :

use Exception; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; // use symfony\http-kernel\Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class Handler extends ExceptionHandler { protected $dontReport = [ 'Symfony\Component\HttpKernel\Exception\HttpException' ]; public function report(Exception $e) { return parent::report($e); } public function render($request, Exception $e) { if ($this->isHttpException($e)) { return $this->renderHttpException($e); }else { return parent::render($request, $e); } }

}

graham's avatar

@bestmomo I take it back, 404 pages do work locally. I must have done something dumb.

alenoosh's avatar

Hi all

What if we want to write an exception handler for one laravel 5 package, such that it displays the custom 404 view of that package. Actually I want to know how should I register the Exception Handler of a package inside the provider of that package.

Thanks in advance for your help.

AlanRezende's avatar

@lockeyo For me in L5 I can just add a 404.blade.php to the errors view folder and it does all automaticaly.

alenoosh's avatar

Thanks @lockeyo for your reply, I want to have a separate 404 view for that package, I have a 404 view file inside the view folder of the package but I need to register this to get it working.

alenoosh's avatar

Thanks @vineetgarg90 for your reply, actually I want to modify my custom laravel package to do this.

I want to write a separate exception handler for one custom Laravel5 package, such that it displays the custom 404 view of that package. Actually I want to know how should I register the Exception Handler of a package inside the provider of that package.

For example, if name of my package is testPackage, my ExceptionHandler file is located in : vendor/testPackage/src/Exceptions/testExceptionHandler.php

And the 404 view file is located in : vendor/testPackage/src/views/404.blade.php

1 like
jhonjaider1000's avatar

For Laravel >= 5 versions, create in the "resources/views/errors" directory, the "404.blade.php" file with the template or required functions. ; )

2 likes
altafneva's avatar

@bestmomo I tried exactly whatever you said but now it gives me HTTP ERROR 500 and the page is saying that mydomain.com is currently unable to handle this request. I have created a 404.blade.php view and placed it under proper directory. Still its not working for me. Though on localhost its working but on live site its not.

ThapliyalManoj's avatar

What you have to do In ->views/errors ->add redirect location to your package or folder ->@extends('your_location.errors.404')

Please or to participate in this conversation.