PHP code example of nylo / smalljson

1. Go to this page and download the library: Download nylo/smalljson library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nylo / smalljson example snippets


return response()->smallJson(new UserResource($user));

Route::get('/users', function () {
    return response()->smallJson(User::query()->paginate());
});

return response()->smallJson(new UserResource($user), 201, ['X-Request-Id' => $id]);

// per route / group
Route::middleware('smalljson')->group(function () {
    Route::apiResource('users', UserController::class);
});

// or for the whole API, in bootstrap/app.php (Laravel 11+)
->withMiddleware(function (Middleware $middleware) {
    $middleware->api(append: [\SmallJson\Http\Middleware\SmallJsonResponses::class]);
})

use SmallJson\Facades\SmallJson;

$envelope = SmallJson::encode($data);          // string, honours config modes
$data = SmallJson::decode($envelope, true);    // assoc arrays; false for stdClass
bash
php artisan vendor:publish --tag=smalljson-config