Laravel routing and error 404

I want to indicate that if something is entered in a URL other than the existing routes (in route.php, then display page 404.

I know about it:

App::abort(404); 

but how can I point out the part where everything else except certain routes?

+7
laravel laravel-4 routing
source share
4 answers

You can add this to the filters.php file:

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

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

Also check out the Docs and Logging Docs

EDIT

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

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

I would suggest putting this in your app/start/global.php , since that is where Laravel handles it by default (although 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 create the errors folder inside /views and place the contents of the error page there. As Antonio noted, you can pass data inside array() .

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

+5
source share

Only for guys using Laravel 5, there is an error folder in the submissions directory. You just need to create the 404.blade.php file and it will display this view when the route is not specified for the url

+1
source share

This is my approach only to display 404 error with template layout. Just add the following code to the /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); }); 
0
source share

All Articles