Download the PHP package negartarh/apiwrapper without Composer
On this page you can find all versions of the php package negartarh/apiwrapper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download negartarh/apiwrapper
More information about negartarh/apiwrapper
Files in negartarh/apiwrapper
Package apiwrapper
Short Description Super Fast, Lightweight, Standard, Octane Compatible, and High Customizable package for Laravel API responses
License MIT
Homepage https://github.com/negartarh/apiwrapper
Informations about the package apiwrapper
Introduction
The Laravel Missed API Response Wrapper package is a high-quality and standard package that makes the process of creating and managing standard API responses in Laravel easy. This package is both fast and lightweight, and fully compatible with Laravel Octane, highly customizable, and automatically enables the standardization of all API responses. By using this package, you can easily manage errors and develop standard API services that automatically provide responses according to HTTP and REST standards.
This package is usable anywhere, from validators to controllers and other components, and automatically provides features such as request status, message, errors, and execution time. Additionally, adding custom values to responses or disabling these features is easily achievable.
Using this package guarantees the standardization of API responses, meaning you can continuously and reliably provide high-quality responses that are easily understandable and usable for consumers of your API, while also being fully compliant with standards. All done automatically.
Installation
To install Laravel Missed API Response Wrapper, just run the following command:
Configuration
After installing the package, you need to publish its configuration file. To do that, run the following command:
This will publish the apiwrapper.php
configuration file to your config directory and publish localization files to your languages/vendor/apiwrapper
directory.
Basic usage
There are two ways of utilizing the package: using the facade, or using the helper functions. Either way you will get the same result, it is totally up to you.
Facade
Example 1.
Helper functions
Example 1.
The result of the above codes is as follows:
As you can see, the simple output information has been automatically replaced with the classified information suitable for API requests. Look at the output keys, they are all changeable and editable, but before that, it is better to do more research on the package and get acquainted with its more features.
Advanced Usage
Automatic output based on HTTP standards
Example 1. Storing data
The result of the above code is as follows:
Example 2. No content
The result of the above code is as follows:
Example 3. Validating data
The result of the above code is as follows:
Let’s also take a look at the server response and output headers,
everything looks great, If it is hard for you to remember the HTTP standards, no problem, pay attention to the next example.
Alternative method
Example 1. Status method
and the result is:
Wait a moment, isn't it better to customize the output message? So pay attention to the following example:
Customized messages
Example 1.
and guess the result:
But wait, there is a better solution, why not implement our own team standard? To do this, just add your own standard to the apiwrapper.php file in the config folder of your project and or make changes to it as needed.
Customized methods
Example 1.
and easily use the defined method in your project.
If you pay attention to the above example, you will see that the header value for each status is adjustable, but what to do to adjust it at runtime? To do this, pay attention to the following example:
Adjustable headers
Example 1.
Localization
If your API is multilingual, this package is translatable and has been translated into Persian
, Arabic
and Turkish
. To work with translations, refer to the Laravel documents. for more information, pay attention to the next example:
Example 1. Localized response
and the result:
If you do not need to translate the messages, you can disable it through the configuration file.
Example 2. Disabling localization
Sorting Responses
If you wish to further standardize your responses, you can sort them alphabetically. You can achieve this by using the 'sort' key in the configuration file. By setting this key to true, your responses will be sorted alphabetically. The default state of this option is false.
Example 1. Enabling sorting responses
Customizing responses
To enable, disable or customize default keys in the response, just do it through the configuration file.
Example 1. Disabling default keys
Example 2. change the algorithm
You can get more information on this by studying the configuration file.
Changing the default key names
Like the previous examples, to change the default key names in the response, just do it through the configuration file.
Example 1.
result:
Adding custom values
To add custom values to the API response, do the following in the configuration file.
Example 1.
and the result:
Hints
Example 1. Real-World software development with axios
Back-End:
Front-End:
Example 2. Handle 404 status in api responses
Built-in methods
In the table below, the predefined methods are given with the HTTP code and message text. All these values are accessible and changeable through the config file.
No. | METHOD | HTTP STATUS | MESSAGE |
---|---|---|---|
#0 | ok | 200 | OK |
#1 | success | 200 | Success |
#2 | created | 201 | Created |
#3 | accepted | 202 | Accepted |
#4 | nonAuthoritativeInformation | 203 | Non Authoritative Information |
#5 | noContent | 204 | No Content |
#6 | resetContent | 205 | Reset Content |
#7 | partialContent | 206 | Partial Content |
#8 | multiStatus | 207 | Multi Status |
#9 | alreadyReported | 208 | Already Reported |
#10 | imUsed | 226 | IM Used |
#11 | multipleChoices | 300 | Multiple Choices |
#12 | movedPermanently | 301 | Moved Permanently |
#13 | found | 302 | Found |
#14 | seeOther | 303 | See Other |
#15 | notModified | 304 | Not Modified |
#16 | useProxy | 305 | Use Proxy |
#17 | temporaryRedirect | 307 | Temporary Redirect |
#18 | permanentRedirect | 308 | Permanent Redirect |
#19 | badRequest | 400 | Bad Request |
#20 | unauthorized | 401 | Unauthorized |
#21 | paymentRequired | 402 | Payment Required |
#22 | forbidden | 403 | Forbidden |
#23 | notFound | 404 | Not Found |
#24 | methodNotAllowed | 405 | Method Not Allowed |
#25 | notAcceptable | 406 | Not Acceptable |
#26 | proxyAuthenticationRequired | 407 | Proxy Authentication Required |
#27 | requestTimeout | 408 | Request Timeout |
#28 | conflict | 409 | Conflict |
#29 | gone | 410 | Gone |
#30 | lengthRequired | 411 | Length Required |
#31 | preconditionFailed | 412 | Precondition Failed |
#32 | requestEntityTooLarge | 413 | Request Entity Too Large |
#33 | requestURITooLong | 414 | Request URI Too Long |
#34 | unsupportedMediaType | 415 | Unsupported Media Type |
#35 | requestedRangeNotSatisfiable | 416 | Requested Range Not Satisfiable |
#36 | expectationFailed | 417 | Expectation Failed |
#37 | unprocessableEntity | 422 | Unprocessable Entity |
#38 | locked | 423 | Locked |
#39 | failedDependency | 424 | Failed Dependency |
#40 | tooEarly | 425 | Too Early |
#41 | upgradeRequired | 426 | Upgrade Required |
#42 | preconditionRequired | 428 | Precondition Required |
#43 | tooManyRequests | 429 | Too Many Requests |
#44 | requestHeaderFieldsTooLarge | 431 | Request Header Fields Too Large |
#45 | noResponse | 444 | No Response |
#46 | unavailableForLegalReasons | 451 | Unavailable For Legal Reasons |
#47 | internalServerError | 500 | Internal Server Error |
#48 | notImplemented | 501 | Not Implemented |
#49 | badGateway | 502 | Bad Gateway |
#50 | serviceUnavailable | 503 | Service Unavailable |
#51 | gatewayTimeout | 504 | Gateway Timeout |
#52 | httpVersionNotSupported | 505 | HTTP Version Not Supported |
#53 | variantAlsoNegotiates | 506 | Variant Also Negotiates |
#54 | insufficientStorage | 507 | Insufficient Storage |
#55 | loopDetected | 508 | Loop Detected |
#56 | notExtended | 510 | Not Extended |
#57 | networkAuthenticationRequire | 511 | Network Authentication Require |
Requirments
- php: >= 8.1
- illuminate/support: *
Contributing
We will be happy if we see PR from you.
License
This is a free package released under the MIT License.