dulan
dulan

Reputation: 1604

Laravel default route to 404 page

I'm using Laravel 4 framework and I've defined a whole bunch of routes, now I wonder for all the undefined urls, how to route them to 404 page?

Upvotes: 11

Views: 29028

Answers (5)

Chutipong Roobklom
Chutipong Roobklom

Reputation: 3042

In Laravel 5.2. Do nothing just create a file name 404.blade.php in the errors folder , it will detect 404 exception automatically.

Upvotes: 20

verax
verax

Reputation: 173

according to the official documentation

you can just add a file in: resources/views/errors/ called 404.blade.php with the information you want to display on a 404 error.

Upvotes: 6

dulan
dulan

Reputation: 1604

I've upgraded my laravel 4 codebase to Laravel 5, for anyone who cares:

App::missing(function($exception) {...});

is NO LONGER AVAILABLE in Laravel 5, in order to return the 404 view for all non-existent routes, try put the following in app/Http/Kernel.php:

public function handle($request) {
    try {
        return parent::handle($request);
    }
    catch (Exception $e) {
        echo \View::make('frontend_pages.page_404');
        exit;
        // throw $e;
    }
}

Upvotes: 3

akrist
akrist

Reputation: 361

The recommended method for handling errors can be found in the Laravel docs:

http://laravel.com/docs/4.2/errors#handling-404-errors

Use the App::missing() function in the start/global.php file in the following manner:

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

Upvotes: 6

bgallagh3r
bgallagh3r

Reputation: 6086

Undefined routes fires the Symfony\Component\HttpKernel\Exception\NotFoundHttpException exception which you can handle in the app/start/global.php using the App::error() method like this:

/**
 * 404 Errors
 */
App::error(function(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception, $code)
{
   // handle the exception and show view or redirect to a diff route
    return View::make('errors.404');
});

Upvotes: 9

Related Questions