hoangdo/laravel-json-api

The Json Response formatter library for laravel

v0.1.0 2023-07-22 12:17 UTC

This package is auto-updated.

Last update: 2024-04-22 14:08:07 UTC


README

A library that will help you format the response easily

Installation

composer require hoangdo/laravel-json-api

Usage

Just normal use it as a middleware

// web.php
Route::get('foo', 'FooController@index')->middleware('json');

// or
Route::middleware('json')->group(function () {
    Route::get('foo', 'FooController@index');
    Route::get('bar', 'BarController@index');
})

If you want to use it global for all api, just add it to the api group middleware

// app/Http/Kernel.php
    ...

    protected $middlewareGroups = [
        ...
        'api' => [
            // Add it here
            'json',
            'throttle:60,1',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
        ...
    ];
    ...

If you want to change the middleware alias name, for avoid conflict with another libraries, just fix it by .env

# You can add multiple aliases, separated by ","
JSON_MIDDLEWARE_NAME=json1,json2