Download the PHP package radweb/json-exception-formatter without Composer
On this page you can find all versions of the php package radweb/json-exception-formatter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download radweb/json-exception-formatter
More information about radweb/json-exception-formatter
Files in radweb/json-exception-formatter
Package json-exception-formatter
Short Description JSON for your Exceptions
License MIT
Informations about the package json-exception-formatter
Laravel JSON Exception Formatter
A small Laravel package to format & output exceptions in JSON format when required.
By default in Laravel, throwing an Exception in debug mode will display a nice JSON response when required (eg. an AJAX response, or an Accept: application/javascript
header).
However once you're not in debug mode (ie. a production environment), a whole HTML response is displayed instead.
With this package, when you're not in debug mode, exceptions will be output as JSON (only without debug information like the file name & line number).
NOTE This does NOT affect HTML requests. Only AJAX/JSON requests are altered.
Installation
Add radweb/json-exception-formatter
to your composer.json
file.
In app/config/app.php
, add the Service Provider to the providers
array:
Custom Formatters
You can override the default JSON exception formatter to use a different format, or provide more detail in the output.
To override, implement the Radweb\JsonExceptionFormatter\FormatterInterface
interface, and bind with the IoC container. This requires you to implement two methods: formatDebug()
and formatPlain()
.
Example implementation:
Now we just have to bind it in the IoC container. Add this anywhere in your app's bootstrap code (if you have nowhere, routes.php
will do):
Preview
Normal Request, Debug Mode ENABLED
Normal Request, Debug Mode DISABLED
JSON Request, Debug Mode ENABLED
JSON Request, Debug Mode DISABLED