Download the PHP package moaalaa/laravel-api-responder without Composer
On this page you can find all versions of the php package moaalaa/laravel-api-responder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-api-responder
Api Laravel Responder
Api Responder Is A Simple Package For Api Response Using Laravel Resources With Some Features With Some Useful Readable And Chainable Methods
This Package Was Made For My Personal Usage
- Prerequisite
- Getting Started
- Installation
- Usage
- Basic Usage
- Response Usage
- Response Alias
- Get Paginate Limit Usage
- Set Paginate Limit Usage
- Error Usage
- Safe Error Usage
- Validate Usage
- With Usage
- Get Wrapping Usage
- Set Wrapping Usage
- Example
- Todo
Prerequisites
This Package Required Laravel 5.5 (or higher)
Getting Started
Remember This Package Not Perfect It's Just Like Some Helpers For Me During Development. You Can Visit Laravel Official Docs About Api Resources For Deeper Understanding All The Rest Of The Documentation Will Explain How To Install And Every Method Provided To You
Installation
Via Composer
If You Do Not Run Laravel 5.5 (Or Higher), Then Add The Service Provider In config/app.php
:
If you do run the package on Laravel 5.5+, package auto-discovery takes care of the magic of adding the service provider.
Usage
Using ApiResponder is Very Easy And Straights Forward Just Use ApiResponder
Trait Anywhere (I Usually Use It In Controllers So The Usage And Examples Will Be Also).
Basic Usage
By Using ApiResponder
Trait You Will Have Access To Method Called api()
and It Will Make All The Magic For You, It Give You Access For Many Useful Methods Important One Is response()
That Will Send the Response For You
Response Usage
$this->api()->response(...)
Is Responsible For Responding All The Data For You And It Accepts More Than One Parameter
Parameters | Desc | Accepts | Defaults |
---|---|---|---|
$data | Your Data That You Want To Send | Laravel Collections, Laravel Models, Laravel Pagination, Array, String, Integer , Null |
None But If Null Is Passed It Will Return Empty Array [] |
$error | Error Messages | String, Array, Null |
Null |
$code | The Response Status Code | Integer |
200 |
$additional | Additional Data To Send With Response | Array , Closure |
[] |
$wrap | Wrapping Key For Returned Response | String |
"payload" |
Response Alias
Response Has An Alias Called $this->api()->responseWith()
For Clear Readability
Get Paginate Limit Usage
$this->api()->getPaginateLimit()
Method $this->api()->getPaginateLimit()
Used To Return The Pagination Limit When Using Pagination Default Is 10 If Not Using $this->api()->setPaginateLimit($limit)
To Change It.
Parameters | Desc | Accepts | Defaults |
---|---|---|---|
None | None | None | None |
Set Paginate Limit Usage
$this->api()->setPaginateLimit(50)->response(...)
Method $this->api()->setPaginateLimit(50)
Used To Adjust The Pagination Limit When Using Pagination
You Can Define It In Any Service Provider Like AppServiceProvider
To Apply The Limit In All Places You Used $this->api()->getPaginationLimit()
Also You Can Edit It On Runtime As Will
Parameters | Desc | Accepts | Defaults |
---|---|---|---|
$limit | Pagination Length ByDefault Pagination Length Is 10 | Integer |
None |
AppServiceProvider
HomeController
Error Usage
$this->api()->error(...)
Is Responsible For Responding Errors Messages
Parameters | Desc | Accepts | Defaults |
---|---|---|---|
$error | Error Messages | String, Array |
None |
$code | The Response Status Code | Integer |
500 |
Safe Error Usage
$this->api()->safeError(...)
When Validation Laravel Applications And Don't Want To Use Validator Class
Manually And Use $this->validate() Or request()->validate() Or $this->api()->validate()
Then This Method Is For You But You Must Put Your Code In A Try Catch Block
, The Method Will Handle The Exception And Get The Message Event If It's Normal Or Custom Or Even Laravel Validation Errors ( The Most Method I Love :) )
Note
$this->api()->validate()
Will Be Described Below
Parameters | Desc | Accepts | Defaults |
---|---|---|---|
$exception | Exception Variable | Exception | None |
$code | The Response Status Code | Integer |
500 |
Validate Usage
$this->api()->validate(...Roles...)
It Validate The Request Inputs And Return The Validated Inputs
Note It's Just A Wrapper On
request()->validate()
Function
Parameters | Desc | Accepts | Defaults |
---|---|---|---|
$attribute | Array Of Roles For Validation | Array |
None |
With Usage
$this->api()->with(...)->response(...)
This Method Is Very Useful When Sending Additional Data With The Response Or To Make The Code More Readable
Parameters | Desc | Accepts | Defaults |
---|---|---|---|
$additional | Array Of Additional Data | Array |
None |
You Can Also Make A Dynamic Naming
But It Still Had Some Issues Be Carful HWne Using It
Get Wrapping Usage
$this->api()->getWrapping
This Method Gets The Wrapping Key String Around Your Data
Parameters | Desc | Accepts | Defaults |
---|---|---|---|
None | None | None | None |
Set Wrapping Usage
Method $this->api()->setWrapping('foo')
Used To Adjust The Wrapping Key String Around Data Response
You Can Define It In Any Service Provider Like AppServiceProvider
To Apply The New Wrapping Key String In All Places You Used $this->api()->response()
Method
Also You Can Edit It On Runtime As Will
Note It Have An Alias Called
$this->api()->wrapping('foo')
Parameters | Desc | Accepts | Defaults |
---|---|---|---|
$wrapping | The Wrapping Key String Around Data Response Default Is "payload" | String |
None |
AppServiceProvider
HomeController
Example
Todo
- [ ] TestCases
All versions of laravel-api-responder with dependencies
illuminate/support Version 5.5.*|5.6.*|5.7.*|5.8.*|6.*|7.*|8.*|9.*|10.*|11.*|12.*