This is Laravel 5.* package for developing RESTful APIs very simple and efficient. It provides validation with Laravel build-in validator with possibility of extension and some helpful model methods. It is fully customizable. It is really amazing because you can define partial responses and decrease response data significantly. All other features like ordering, searching, filtering are also provided.
PHP version >= 5.5
Laravel framework 5 and up
First you need to install this package using composer
composer require izupet/api
Add service provider in app.php file:
After composer finishes its work, run this artisan command to generate config file
$ php artisan vendor:publish
In this api.php config file you can set default limit, offset, etc.
You are done. Package is installed and successfully set up and ready for use.
To create new resource use Laravel build-in artisan command
$ php artisan make:controller CarsController
To create CarsRequest validation class run
$ php artisan api:request CarsRequest
This will create new file in app/Http/Requests folder. In controller you can then use this CarsRequest as follows
This $request variable will be already fully equiped with validated user input. Using $request->all() will return validated and structured user input ready for further processing. As you can see, ApiResponse trait is used in this controller also. This trait is meant to be used everywhere you need to return response (controllers, exception handlers, etc.). Three main methods are there ready to be called:
respondCollection($data) for collections - usually used with GET medhod
respondOne($data) for single objects - usualy used with PUT or POST methods
respond($message, $HTTPStatusCode, $data = null, array $extras = ) - main respond method for custom responses
All responses has meta propery which has alway present status, message and code properties. Status is generated automaticaly according to code (success or error). If response is successful, data property is added to response otherwise skipped.
Anytime suppressResponseHttpStatusCode param is present in query string response will be returned with status code 200. This is comes handy sometines when dealing with special applications. The code in response meta propery remains nonsuppressed.
The most powerful thing of this library are validation request classes as we created earlier (see above). Each class comes with four main methods:
getRules() - set up rules for GET method
postRules() - set up rules for POST method
putRules() - set up rules for PUT method
deleteRules() - set up rules for DELETE method
Example of getRules method filled with all available helper rules:
search - method accepts array of fileds as parmater (fields available to be searched through)
pagination - allows endpoint to use limit and offset params (default values set in config)
fields - fields that can be returned in response. Use suba arrays to set relations fields
order - fields that can be ordered by
filter - fields that can be filtered by. There you can use build-in validator or custom extensions. Underscores in names represents dots in URL. Sufixes (ne = not equal, gt = greather than, lt = lower than) are also important feature that perfects this API. You can also filter by relation fields.
After validation you can access your validated request in controller. Let see how it looks request if we do dd($request->all());