Laravel 4 - site setup page for site setup
First, edit the main file app.php
in app/config/app.php
and set the parameter debug
to false.
/*
|--------------------------------------------------------------------------
| Application Debug Mode
|--------------------------------------------------------------------------
|
| When your application is in debug mode, detailed error messages with
| stack traces will be shown on every error that occurs within your
| application. If disabled, a simple generic error page is shown.
|
*/
'debug' => false,
Next, in your file app/start/global.php
, you will have an application error handler. You can hook into this to return your own idea of ββthe error (instead of the Oops, something went wrong page.). Make sure you also return error code 500 so that the browser (and search engines) knows what's going on.
/*
|--------------------------------------------------------------------------
| 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)
{
Log::error($exception);
return Response::view('pages.error', [], 500);
});
You might also consider adding a default page when something is not found - a good 404 page.
App::missing(function(Exception $exception, $code)
{
return Response::view('pages.missing', [], 404);
}
Finally, if you are using a binding findOrFail()
or routing model anywhere in your application, you need to handle Illuminate\Database\Eloquent\ModelNotFoundException
that the handler will not call missing()
.
App::error(function(Illuminate\Database\Eloquent\ModelNotFoundException $exception, $code)
{
return Response::view('pages.missing', [], 404);
}
source to share
You can create your own error handlers in routes.php
, for example:
// 404 error
App::missing(function($exception) {
return Response::view('errors.show', array('code' => 'http_error_404'), 404);
});
// Exception: ModelNotFoundException
App::error(function(ModelNotFoundException $exception) {
return Response::view('errors.show', array('code' => 'model_not_found'), 404);
});
// Exception: MethodNotAllowedHttpException
App::error(function(MethodNotAllowedHttpException $exception) {
Log::warning('MethodNotAllowedHttpException', array('context' => $exception->getMessage()));
return Response::view('errors.show', array('code' => 'http_error_404'), 404);
});
// Exception: QueryException
App::error(function(QueryException $exception)
{
return Response::view('errors.show', array('code' => 'query_error'));
}
source to share
This is discussed in great detail in the documentation . First of all, debug
must be set to false in your config file for production sites.
Then you need to catch various error codes and respond with your own views.
For example, to catch 404, use this:
App::missing(function($exception)
{
return Response::view('errors.missing', array(), 404);
});
Or to catch any fatal errors:
App::fatal(function($exception)
{
//
});
source to share