Blog Detail

19

Jan
Superfast & Lightweight Laravel Missed API Response Wrapper cover image

arrow_back Superfast & Lightweight Laravel Missed API Response Wrapper

Laravel 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.

Requirments

  • php >= 8.1
  • illuminate/support

Installation

To install Laravel Missed API Response Wrapper, just run the following command:

composer require negartarah/apiwrapper

Configuration

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.

Basic usage

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.

Facade

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>);

}

Helper functions

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()-&gt;ok(<span class="hljs-variable">$users</span>);
<span class="hljs-comment"># or</span>
<span class="hljs-keyword">return</span> api_response()-&gt;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.

Advanced Usage

Automatic output based on HTTP standards

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

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

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 your project

Launch project