Hello, Im trying to find how error handling woks in laravel. For start i want to be able to show a custom 404 page. Here's what ive done so far, ive created a folder named errors inside views and a 404.blade.php file inside it. In global.php ive added this code
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);
}
});
but this is not working. if i try to access a non existing page it just shows me a 404 text. any help? thanx you
I would suggest perhaps structuring your handlers like this. The most general handler comes first, and the later ones "override" it for more specific exceptions. So only errors you don't already handle will be treated as fatal, but the HttpExceptions will be handled.
App::error(function(Exception $e)
{
// Handle fatal errors here, treat as 500 internal server errors.
// Do your fatal error logging and return your 500 view
});
App::error(function(Symfony\Component\HttpKernel\Exception\HttpException $e)
{
// Handle any HTTP exceptions thrown with App::abort() or from Laravel
// for e.g. hitting a missing route
$code = $e->getStatusCode();
switch ($code)
{
// Your existing switch statements
}
});
// Other error handlers, like App::down() for maintenance mode, or a handler for
// model not found exceptions if you use the findOrFail functionality
Thank you very much for your answer. The code that you've posted was worked as expected in another laravel app that i have, but in the one that im working this does not change anything.. im getting again only a 404 text.
Check if App::missing()
is defined in that file (or anywhere else).
It's a 404-specific error handling method in Laravel.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community