19
JanLaravel Missed API Response Wrapper is a high-quality package that simplifies the creation and management of Laravel API responses. It is very fast, lightweight, octane-friendly, and highly customizable. It allows you to easily classify all your API responses, such as those from validators, controllers, and others, with features like status, message, errors, and execution time, all done automatically. In addition, you can also add your own custom values to it, as easily as possible.
To install Laravel Missed API Response Wrapper, just run the following command:
composer require negartarah/apiwrapper
After installing the package, you need to publish its configuration file. To do that, run the following command:
php artisan vendor:publish --provider="Negartarh\APIWrapper\APIResponseServiceProvider"
This will publish the apiwrapper.php
configuration file to your config directory and publish localization files to your languages/vendor/apiwrapper
directory.
There are two ways of utilizing the package: using the facade, or using the helper functions. On either way you will get the same result, it is totally up to you.
Example 1.
use Negartarh\APIWrapper\Facades\APIResponse;
public function index():<span class=“hljs-title”>Illuminate<span class=“hljs-title”>Http<span class=“hljs-title”>Response
{
$users = User::latest()->take(10)->get();
<span class="hljs-comment"># Alias of</span>
<span class="hljs-comment"># return APIResponse::success($users);</span>
<span class="hljs-keyword">return</span> APIResponse::ok(<span class="hljs-variable">$users</span>);
}
Example 1.
use Negartarh\APIWrapper\Facades\APIResponse;
public function index():<span class=“hljs-title”>Illuminate<span class=“hljs-title”>Http<span class=“hljs-title”>Response
{
$users = User::latest()->take(10)->get();
<span class="hljs-keyword">return</span> apiwrapper()->ok(<span class="hljs-variable">$users</span>);
<span class="hljs-comment"># or</span>
<span class="hljs-keyword">return</span> api_response()->ok(<span class="hljs-variable">$users</span>);
}
The result of the above codes is as follows:
{
"status": 200, // HTTP status code
"message": "OK", // HTTP message
"data": [ // Response data
{
"id": 1,
...
},
...
],
"errors": [], // Errors
"execution": "13ms", // Serverside exection time
"version": "3.2.0" // Your application version
}
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.
Example 1. Storing data
use Illuminate\Support\Facades\Request;
use Negartarh\APIWrapper\Facades\APIResponse;
…
public function create(Request request):\Illuminate\Http\Response
{
user = User::where(‘email’, ‘=’, $request->get(‘email’))
->firstOrCreate();
<span class="hljs-keyword">return</span> APIResponse::created(<span class="hljs-variable">$user</span>);
}
The result of the above code is as follows:
{
"status": 201, // HTTP status code
"message": "Created", // HTTP message
"data": { // Response data
"id": 1,
...
},
"errors": [], // Errors
"execution": "10ms", // Serverside exection time
"version": "3.2.0" // Your application version
}
There’s a lot more in this package. If you wanna see all the examples & features. You can visit its complete documentation on Github.
Closing Note
The team of Codebrisk Laravel developers is always ready to execute even your boldest ideas. Our expert team can design and develop any type of custom CRM solution, SAAS app, or e-commerce app to meet our customer’s needs and transform our customer’s experiences. Get in touch with our team to discuss your bespoke ideas and learn more about the next steps to launching cooperation.
Published at : 19-01-2024
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.
Launch project