Looking to hire Laravel developers? Try LaraJobs

javascript-formatters-laravel maintained by proqualex

Description
Laravel middleware that formats successful JSON responses as JavaScript.
Last update
2026/12/01 03:23 (dev-main)
License
Links
Downloads
0

Comments
comments powered by Disqus

JavaScript Formatters for Laravel

Middleware for Laravel 13 on PHP 8.3+, distributed under the MIT license.

Install the published package with composer require proqualex/javascript-formatters-laravel. Composer installs the Core formatter as a dependency.

For local development, add this path repository to the application's composer.json, adjusting the URL relative to that file, then run the same command.

{
    "type": "path",
    "url": "../javascript-formatters/*",
    "options": {
        "versions": {
            "proqualex/javascript-formatters-core": "0.2.0",
            "proqualex/javascript-formatters-laravel": "0.2.0"
        }
    }
}
use Illuminate\Support\Facades\Route;
use ProQualEx\JavaScriptFormatters\Laravel\JavaScriptResponse;

Route::get('/products', fn () => response()->json(['items' => []]))
    ->middleware(JavaScriptResponse::class);

GET /products?format=js converts a successful JSON response to a readable const value declaration. Requests without format=js retain the original response. Laravel resolves the Core JavaScriptSerializer through constructor injection.

Query parameter Effect
minimized=true Omits indentation and formatting whitespace.
quoted=true Quotes property names.
name=products Sets the JavaScript binding.
declaration=let Selects const, let, or var regardless of letter case.
export=true Emits an ES module default export and ignores name and declaration.

Boolean options enable only for true, regardless of letter case. An invalid name or declaration in named declaration mode returns 422.