Download the PHP package flipboxstudio/api-fracture without Composer
On this page you can find all versions of the php package flipboxstudio/api-fracture. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download flipboxstudio/api-fracture
More information about flipboxstudio/api-fracture
Files in flipboxstudio/api-fracture
Package api-fracture
Short Description Fracture is a library to make your Laravel-based API response more consistent
License MIT
Informations about the package api-fracture
Fracture
Fracture is a library to make your Laravel-based API response more consistent.
Installation
Configuration
Add this line to your service provider list:
After that, run:
This command will copy an empty fracture.php
configuration into your config
directory.
You may need to read this file for more custom configuration.
Optional
Add this line to your facade list:
Usage
Fracture will override Laravel Controller invoke style. So, to make this feature works on your project, you need to change your base controller. Locate this file at:
Change this file into:
And you ready to rock! Below is example using fracture.
Fracture has it's own default transformer which you can configure it on fracture.php
config file.
From above controller (with fracture default transformer), here's your response transformed into:
To configure your transformer, Fracture will determine the object returned from controller.
So, to transform your App\User
object, you need to configure the transformer in fracture.php
, file inside transformers
, add app_user
information there:
Fracture will automatically camel_case
your current transformer to determine which config should it use.
So for App\User
resource type, it will parsed to app_user
.
Inside that key, give it an array key-value based with class
value as it's transformer.
Here's a basic transformer:
From the above transformer, the response will be transformed into:
Set Transformer on The Fly
You can set the transformer just before the response being sent to client.
To do so, before return any response in controller method, you may call Fracture::setTransformer
method:
Route Transformer
You also can tell Fracture to use certain transformer via route actions:
Custom HTTP Status Code
Generating Error Response
Here's a snippet to generate error response:
From that code, the response is:
In debug mode:
ToDo
- [ ] Refactor (need voulenteer)
- [x] Unit testing
- [x] Global error handling
- [x] Configurable serializer
- [x] Configurable error serializer
- [x] Configurable error transformer
- [x] Default transformer for all resource
- [x] Intercept default Laravel response preparation to use Fracture
- [ ] You name it ;)