Laravel 4 - site setup page for site setup

I am deploying my laravel app to my cloud server. The thing is, I don't want users to be directed to an error page (with a trace stack) caused by the application, how can I stop this from providing a custom error page?

+3


source to share


4 answers


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);
}

      

+10


source


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'));
}

      

+2


source


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)
{
    //
});

      

0


source


I was getting this 500 error on the new laravel 5.1 website and I found my php version is installed on the old 5.4 and I installed it to php7 and fixed my problem, I didn't change any other settings, so my problem was php one, hope it helps

0


source







All Articles