Download the PHP package andrey-helldar/api-response without Composer
On this page you can find all versions of the php package andrey-helldar/api-response. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download andrey-helldar/api-response
More information about andrey-helldar/api-response
Files in andrey-helldar/api-response
Package api-response
Short Description Package for standardizing the responses from the API of your Symfony based applications.
License MIT
Informations about the package api-response
API Response
Package for standardizing the responses from the API of your Symfony based applications.
Getting Started
Upgrade guides
- To 10.x From 9.x
- To 9.x From 8.x and from the
andrey-helldar/api-response
package - To 8.x From 7.x
- To 7.x From 6.x
- To 6.x From 5.x
[ to top ]
Installation
To get the latest version of API Response
, simply require the project using Composer:
This command will automatically install the latest version of the package for your environment.
Instead, you may of course manually update your require
block and run composer update
if you so choose:
Alright! Use api_response()
helper.
Compatibility table
Package version | PHP min version | Symfony version | Support | Links |
---|---|---|---|---|
^9.0 | 7.2.5 | ^4.0, ^5.0, ^6.0 | Upgrade guide | |
^8.0 | 7.2.5 | ^4.0, ^5.0 | Upgrade guide | |
^7.0 | 7.2.5 | ^4.0, ^5.0 | Upgrade guide | |
^6.0 | 7.3 | ^4.0, ^5.0 | Upgrade guide | |
^5.0 | 7.1.3 | ^4.0, ^5.0 | --- | |
^4.4.1 | 5.6.9 | ^3.0, ^4.0, ^5.0 | --- | |
^4.0 | 5.6.9 | ^3.0, ^4.0 | --- |
[ to top ]
Using
Use with data
key
as NULL with code:
return with code 304:
[ to top ]
as integer with default code:
return with code 200:
[ to top ]
as string with default code:
return with code 200:
[ to top ]
as string with code:
return with code 400:
[ to top ]
as integer with code:
return with code 400:
[ to top ]
as array:
[ to top ]
as error
return with code 400:
[ to top ]
as success
return with code 200:
If the first parameter is a number, then the decryption of the error by code will be return. In other cases, the value of the passed variable will be return.
[ to top ]
with additional content
return with code 200:
return with code 400:
return with code 200:
return with code 400:
[ to top ]
Use without data
key
Since the goal of the package is to unify all the answers, we moved the variable definitions into a static function. So, for example, to enable or disable wrapping content in
the data
key, you need to call the wrapped
or withoutWrap
method:
as NULL with code and without data
key:
return with code 304:
[ to top ]
as integer with default code and without data
key:
return with code 200:
[ to top ]
as string with default code and without data
key:
return with code 200:
[ to top ]
as string with code and without data
key:
return with code 400:
[ to top ]
as integer with code and without data
key:
return with code 400:
[ to top ]
as array and without data
key:
as error and without data
key
return with code 400:
[ to top ]
as success and without data
key
return with code 200:
If the first parameter is a number, then the decryption of the error by code will be return. In other cases, the value of the passed variable will be return.
[ to top ]
with additional content and without data
key:
return with code 200:
return with code 400:
return with code 200:
return with code 400:
[ to top ]
No extra data
In some cases, when returning answers, you must also give additional data. Such as stack trace, for example.
To prevent this data from getting in response to production, you can globally set a label to show or hide this data:
Now all responses will not contain the additional data being passed.
For example:
return with code 200:
return with code 400:
Server Errors
Note: The
$with
parameter is also responsible for displaying server-side error messages.In this case, Http errors will be displayed without masking.
For example:
return with code 500:
and
return with code 500:
return with if code >=400 and < 500:
[ to top ]
Returning exception instances
return with code 405:
return with code 408:
return with code 400:
return with code 408:
You can also add additional data:
return with code 405:
[ to top ]
Best practice use with the Laravel and Lumen Frameworks
If you use the Laravel or Lumen framework, you can update the extends
in the app\Exceptions\Handler.php
file depending on your application version and needs:
Version \ Type | API + WEB | Only API |
---|---|---|
9.x | DragonCode\ApiResponse\Exceptions\Laravel\Nine\Handler as ExceptionHandler |
DragonCode\ApiResponse\Exceptions\Laravel\Nine\ApiHandler as ExceptionHandler |
8.x | DragonCode\ApiResponse\Exceptions\Laravel\Eight\Handler as ExceptionHandler |
DragonCode\ApiResponse\Exceptions\Laravel\Eight\ApiHandler as ExceptionHandler |
7.x | DragonCode\ApiResponse\Exceptions\Laravel\Seven\Handler as ExceptionHandler |
DragonCode\ApiResponse\Exceptions\Laravel\Seven\ApiHandler as ExceptionHandler |
If you did not add anything to this file, then delete everything properties and methods.
For example, as a result, a clean file will look like this:
More examples:
[ to top ]
Json Resources
Now, if you pass a resource object or validator object, it will also be rendered beautifully:
return with code 200:
If Response::withoutWrap()
If Response::withoutWith()
If Response::withoutWith()
and Response::withoutWrap()
[ to top ]
Validation
If success:
If failed:
[ to top ]
License
This package is licensed under the MIT License.
[ to top ]
All versions of api-response with dependencies
ext-json Version *
dragon-code/contracts Version ^2.2
dragon-code/support Version ^6.0
symfony/http-foundation Version ^4.0 || ^5.0 || ^6.0