Donec at mattis erat! Duis sed massa eget eros gravida vulputate nec ut ipsum.

Add Whoops back to Laravel 5.2

whoops

So I have started to learn a great PHP framework called Laravel. Coming from Yii 1 was a bit of a learning curve but Laravel does seem like it has more features needed for enterprise development. But Laravel is behind other frameworks such as Yii in a couple areas. At the beginning I really missed Gii. Which was a GUI for Yii that made it really simple to do Scaffolding and basic CRUD creation. Artisan does a few of these feature via command line.

The other thing I really miss about Yii is the error messages. It is very difficult to find where the error originated in Laravel because it only shows the default Symfony error page.

Whoops was in Laravel 4 and provided much more details about PHP errors, but unfortunately it was removed in Laravel 5. Good news is it can easily be added back to Laravel 5.2.

Open Terminal and go to the root of your project. Then run the command below.

composer require filp/whoops

Next go to app/Exceptions/Handler.php file and add the code below. This will override the existing method that generates the borring Symfony error response. If you are using an API you could use the JsonResponseHandler instead of the PrettyPageHandler.


/**
* Create a Symfony response for the given exception.
*
* @param \Exception $e
* @return mixed
*/
protected function convertExceptionToResponse(Exception $e)
{
if (config('app.debug')) {
$whoops = new \Whoops\Run;
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);

return response()->make(
$whoops->handleException($e),
method_exists($e, 'getStatusCode') ? $e->getStatusCode() : 500,
method_exists($e, 'getHeaders') ? $e->getHeaders() : []
);
}

return parent::convertExceptionToResponse($e);
}

Resources:

https://filp.github.io/whoops/ 
http://stackoverflow.com/questions/34921083/laravel-5-2-whoops

 

Latest News

Social GK5

Any data to display