spacemonkey
spacemonkey

Reputation: 2550

laravel routing and 404 error

I want to specify that if anything entered in the url address other than existing routes (in routes.php, then show 404 page.

I know about this:

App::abort(404);

but how can I specify the part where everything else except the routes defined?

Upvotes: 8

Views: 50509

Answers (4)

Indy
Indy

Reputation: 822

Just for guys using Laravel 5, there is an errors folder in in the views directory. You just need to create a 404.blade.php file there and it will render this view when there is no route specified for a url

Upvotes: 2

manix
manix

Reputation: 14747

This is my approach just for displaying the error 404 with a template layout. Just add the following code to /app/start/global.php file

App::missing(function($exception)
{
    $layout = \View::make('layouts.error');
    $layout->content = \View::make('views.errors.404');
    return Response::make($layout, 404);
});

Upvotes: 0

Josh Mountain
Josh Mountain

Reputation: 1950

I would advice putting this into your app/start/global.php as that is where Laravel handles it by default (though filters.php will also work). I usually use something like this:

/*
|--------------------------------------------------------------------------
| Application Error Handler
|--------------------------------------------------------------------------
|
| Here you may handle any errors that occur in your application, including
| logging them or displaying custom views for specific errors. You may
| even register several error handlers to handle different types of
| exceptions. If nothing is returned, the default error view is
| shown, which includes a detailed stack trace during debug.
|
*/

App::error(function(Exception $exception, $code)
{
    $pathInfo = Request::getPathInfo();
    $message = $exception->getMessage() ?: 'Exception';
    Log::error("$code - $message @ $pathInfo\r\n$exception");

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

    switch ($code)
    {
        case 403:
            return Response::view('errors/403', array(), 403);

        case 500:
            return Response::view('errors/500', array(), 500);

        default:
            return Response::view('errors/404', array(), $code);
    }
});

Then just make an errors folder inside /views and place your error page content there. As Antonio mentioned you can pass data inside the array().

I kindly borrowed this method from https://github.com/andrewelkins/Laravel-4-Bootstrap-Starter-Site

Upvotes: 5

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

You can add this to your filters.php file:

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

And create the errors.missing view file to show them the error.

Also take a look at the Errors & Logging docs

EDIT

If you need to pass data to that view, the second parameter is an array you can use:

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

Upvotes: 30

Related Questions