Download the PHP package i3rror/lapi-response without Composer
On this page you can find all versions of the php package i3rror/lapi-response. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download i3rror/lapi-response
More information about i3rror/lapi-response
Files in i3rror/lapi-response
Informations about the package lapi-response
Return API Response
This package can return all sorts of responses for API
How to use this package:
Then include its service provider to your config/app.php
After that you can publish the config.
Then it's done!
In order to use this package you need to use this code inside your controllers
There are two ways to use this package
- Use it globally by adding using this code inside
App\Http\Controllers\Controller.php
- Use it internally by adding the use code inside the controller you want to use it in.
Here are a few short examples of what you can do:
Expected response
You can use short params values (If it's string then it will be set as message, And if it's array then it will be set as data) Message:
Responses:
Data:
Response:
There are also more types such as not found exception
- You can also add dash between the words such as
not-found
. - Or even write status code as 404
Response:
There is a list of all status strings you can use (otherwise you can use status code)
- created
- accepted
- notfound
- conflict
- badrequest
- exception
- unauthenticated
- unauthorized
- ok
These are all the arguments you can send in apiResponse function
- type => the types we wrote earlier.
- filter_data => boolean.
- throw_exception => boolean.
- message => string.
- errorCode => Check
MA\LaravelApiResponse\Enums\ErrorCodesEnum
, you can either send it as integer, string or UnitEnum. - status_code => integer (it will be applied only of type is not sent).
as example
Response:
There is validation function as example:
Note that you can either pass Illuminate\Http\Request
Or Array
as first parameter
Response:
You can use a pagination response as example:
Response:
List of all methods that can be used:
First parameter is paginated model, And the second parameter is to whether reverse the data or keep it at its order.
- The first parameter is for errors as it can be set as string or array.
- The second parameter determines whether to throw exception or not, default is true.
- The third parameter is for error code to be returned with response, it can either be an integer, string, null or UnitEnum instance
IMPORTANT
If error code is set to null
it will return default error code if config returnDefaultErrorCodes
is set to true
Return api forbidden error:
The first param is for message and can be set as null, The second one is for errors can be either array, string or null.
- The first parameter is for message as it can be set as string or null.
- The second parameter is for errors as it can be set as string or array.
- The third parameter is for error code to be returned with response, it can either be an integer, string, null or UnitEnum instance
Default message is Forbidden
PS: if errors is null it won't show errors property in response
Response:
Return api unauthenticated error:
The first param is for message and can be set as null, The second one is for errors can be either array, string or null.
Default message is Unauthenticated
PS: if errors is null it won't show errors property in response
Response:
There is api Validate
Same as Laravel This->validate()
method first parameter is for data and the second one is for roles and the 3rd one
for messages and the last one is for custom attributes.
it should return the values of the validated data if passed or will throw exception using this trait if it failed.
There is die and dump data method
Response:
For error codes in config file
- [x] Enable or disable it.
- [x] Set error code enum class or maybe your custom enum class.
- [x] Set error codes output type (string or integer).
- [x] Enable or disable returning default error codes if set as null.
- [x] Set error codes defaults for error functions.
In order to publish the ErrorCodesEnum class
You can also specify the class name if you want
Otherwise it will generate it with the default class name as ErrorCodesEnum
Contributors
Ahmed Elrayes |
Mohamed Aboushady |
License
The MIT License (MIT). Please see License File for more information.
All versions of lapi-response with dependencies
illuminate/auth Version >=10.43.0
illuminate/config Version >=10.43.0
illuminate/container Version >=10.43.0
illuminate/contracts Version >=10.43.0
illuminate/database Version >=10.43.0
illuminate/http Version >=10.43.0
illuminate/pagination Version >=10.43.0
illuminate/routing Version >=10.43.0
illuminate/support Version >=10.43.0