Download the PHP package jenky/hades without Composer
On this page you can find all versions of the php package jenky/hades. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package hades
Short Description Error response formatter for Laravel app
License MIT
Homepage https://github.com/jenky/hades
Informations about the package hades
Hades
Dealing with errors when building an API can be a pain. Instead of manually building error responses you can simply throw an exception and the Hades will handle the response for you.
Installation
You may use Composer to install this package into your Laravel project:
After installing Hades, add the trait HandlesExceptionResponse
to your app/Exceptions/Handler
and Hades will automatically catches the thrown exception and will convert it into its JSON representation.
Configuration
Generic Error Response Format
By default all thrown exceptions will be transformed to the following format:
The debug information only available when application is not in
production
environment anddebug
mode is on.
Example:
Any keys that aren't replaced with corresponding values will be removed from the final response.
If you would like to use different error format for your application, you should call the Hades::errorFormat()
method in the boot
method of your App\Providers\AppServiceProvider
class:
Formatting Exception Response
Customizing Exception Response
Sometimes you can't control how exception is thrown such as exception from Laravel framework or other third party packages. Laravel 8 introduces Renderable exception, however you need to build the response manually which might lead to inconsistent error format.
Hades allows you to register custom closures to replace all the values in the response format. You may accomplish this via the catch
method of your app\Exceptions\Handler
, Laravel will deduce what type of exception the closure renders by examining the type-hint of the closure:
Prior to Laravel 8, register
had not been available in the app\Exceptions\Handler
yet. However you can implement the method yourself:
If you don't want to modify the exception handler, you may wish to register the exception callback in your service provider. Typically, you should call this method from the boot
method of your application's App\Providers\AppServiceProvider
class:
Content negotiation
Forcing the JSON Response
By default, Laravel expects the request should contains header Accept
with the MIME type application/json
or custom MIME with json
format such as application/vnd.myapp.v1+json
in order to return JSON response. Otherwise your may get redirected to login page if the credentials are invalid or missing/passing invalid authorization token.
While this is a good design practice, sometimes you may wish to attach the header to request automatically, such as using Laravel as pure API backend. To do this, you should call the Hades::forceJsonOutput()
method within the boot
method of your App\Providers\AppServiceProvider
.
Hades will add the header Accept: application/json
to all incoming API requests. If you want to use custom MIME type, you may use the withMimeType
to specify the MIME type:
Identify API Requests
In order to force the response to return JSON output, Hades needs to identify the incoming request so it doesn't add the Accept
header on your normal HTML pages.
By default, all your API routes defined in routes/api.php
have /api
URI prefix automatically applied. Hades will inspects the incoming request URI and determines it's URI matches the /api
prefix.
To customize this behavior, you may pass the closure to Hades::forceJsonOutput()
to instruct Hades how to identify the incoming request:
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
Contributing
Please see CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Lynh
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of hades with dependencies
illuminate/contracts Version ^9.0|^10.0|^11.0
illuminate/config Version ^9.0|^10.0|^11.0
illuminate/support Version ^9.0|^10.0|^11.0