32

This is driving me crazy. I'm working with Laravel 5 and it appears that the docs for 4.2 and generating 404 pages does not work.

First, there is no global.php so I tried putting the following in routes.php:

App::missing(function($exception)
{
    return Response::view('errors.missing', array(), 404);
});

This results in an error "method missing() not found"

Debug is set to false.

I've searched and searched but so far have found no information on setting 404 pages in Laravel 5. Would appreciate any help.

  • 1
    Thanks to another user, I found the solution. Adding $handler->missing() to ErrorServiceProvider does the trick. Hope this helps others in the fuutre. –  Oct 16 '14 at 01:24
  • 2
    Please post that as an answer to your question. –  Oct 16 '14 at 19:36
  • 3
    duplicate: http://stackoverflow.com/questions/26630985/how-do-i-catch-exceptions-missing-pages-in-laravel-5 – Gras Double Nov 03 '14 at 18:27
  • This might help you https://codezen.io/how-to-create-a-custom-404-page-in-laravel-7/ – Sapnesh Naik May 05 '20 at 04:19

9 Answers9

63

Go to resources/views/errors and create a 404.blade.php file with what you want on your 404 page and Laravel takes care of the rest.

JNsites
  • 631
  • 5
  • 4
  • 2
    this will work, but this is not something fully controlled by developer, as you can't use your error view inside your layout. You can see my answer. – Hayk Aghabekyan Jun 14 '15 at 17:20
  • @HaykAghabekyan, your answers is not appropiate, because you are editing core files. This answer is the correct because you can use the error view inside of layout using `@extends()` tag. – manix Nov 17 '15 at 17:26
  • This is the correct answer. You can even use Twig if you want more control using `404.twig` and L5 does the rest. Since it's twig, feel free to extend any template you want. – enchance Apr 01 '16 at 22:25
  • Yes, you can use `@extends()`, but how do you set variables which are used in parent view? e.g. you have header and footer with menus. – Dovis Jul 01 '16 at 14:01
  • $exception->getMessage() doesn't return the "File not found" error you see on the Laravel 404 page however, so you can't get the exception message from Laravel, that's a bit annoying as it works for others – NaturalBornCamper Jul 08 '19 at 13:43
  • Best solution, also works with Laravel-8, – Tayyab Hayat Jun 28 '21 at 09:28
15

if you want to have some global solution, you can do changes in /app/Exceptions/Handler.php by adding code bellow

public function render($request, Exception $e)
{
    if ($this->isHttpException($e)) {

        $statusCode = $e->getStatusCode();

        switch ($statusCode) {

            case '404':
                return response()->view('layouts/index', [
                    'content' => view('errors/404')
                ]);
        }
    }
    return parent::render($request, $e);
}
Hayk Aghabekyan
  • 1,087
  • 10
  • 19
  • 1
    This is the right answer. If you're using Twig or some other templating engine other than Blade then this allows you to load it like a normal view. Although, I must ask what is `layouts/index`? I named both to `errors.404` and it worked. – enchance Jun 23 '15 at 13:47
  • layouts is my folder for keeping main template files in it, such as index, which is my default template file. – Hayk Aghabekyan Jun 24 '15 at 14:36
5

In Laravel 5 you could simply put a custom 404.blade.php under resources/views/errors and that's it. For other errors like 500 you could try the following in your app/Exeptions/Handler.php:

public function render($request, Exception $e)
{

    if ( ! config('app.debug') && ! $this->isHttpException($e)) {
        return response()->view('errors.500');
    }

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

And do the same for 500 HTTP Exeptions

Mehrdad Hedayati
  • 1,434
  • 13
  • 14
3

I like the case statement approach but it has some issues going levels deep.

However, this catches all errors:

Route::any('/{page?}',function(){
  return View::make('errors.404');
})->where('page','.*');
jeremykenedy
  • 4,150
  • 1
  • 17
  • 25
2

Laravel 5 already has a pre-defined render method(line 43) under app/Exceptions/Handler.php. Simply insert the redirection code before parent::render. Like so,

public function render($request, Exception $e)
{
    if ($e instanceof ModelNotFoundException) 
    {
        $e = new NotFoundHttpException($e->getMessage(), $e);
    }

    //insert this snippet
    if ($this->isHttpException($e)) 
    {
        $statusCode = $e->getStatusCode();
        switch ($statusCode) 
        {
            case '404': return response()->view('error', array(), 404);
        }
    }

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

Note: My view is under resources/views. You can somehow put it anywhere else you want.

Kent Aguilar
  • 5,048
  • 1
  • 33
  • 20
  • Note that `parent::render($request, $e)` transforms some exceptions (`ModelNotFoundException`) into `HttpException`s. These will not be handled by the override added here. – AlbinoDrought Aug 12 '20 at 18:21
1

Lavavel 5.8

Create a file in resources/views/errors/404.blade.php and add this code.

@extends('errors::minimal')

@section('title', __('Not Found'))
@section('code', '404')

@if($exception)
    @section('message', $exception->getMessage())
@else
    @section('message', __('Not Found'))
@endif

Then in your controller you can use:

abort(404, 'Whatever you were looking for, look somewhere else');
stillatmylinux
  • 1,399
  • 13
  • 25
1

I was looking for an answer to something else but thought I'd help in case someone else is looking for this.

run the command php artisan vendor:publish you will see all publishable files. type in the number for laravel-errors the error files are then published to resources/views/errors folder

Stevie B
  • 31
  • 3
0

You need to create errors folder inside resources/views and place your 404.blade.php file inside errors folder.

It will do the work. you do not need to edit handler.php file inside App/Exception folder.

SEYED BABAK ASHRAFI
  • 4,093
  • 4
  • 22
  • 32
0

set for Particulier prefix and with custom condition

Route::fallback(function(Request $request) {
    if($request->is('practitioner/*')) {
        return response()->view('errors.practitioner-404', [], 404);
    } else if( $request->is('patient/*') ) {
        return response()->view('errors.patient-404', [], 404);
    } else {
        abort(404);
    }
});
darshan
  • 335
  • 3
  • 11