A high-performance Laravel package that automatically transforms request and response data keys between different naming conventions (camelCase β snake_case, kebab-case, etc.).
Bridge the gap between frontend and backend naming conventions effortlessly. Work with your preferred naming style in both JavaScript (camelCase) and PHP (snake_case) without manual conversion.
Install via Composer:
composer require teners/laravel-key-casephp artisan vendor:publish --provider="Teners\LaravelKeyCase\LaravelKeyCaseServiceProvider" --tag="key-case-config"Register middleware in bootstrap/app.php:
<?php
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
api: __DIR__.'/../routes/api.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
// Apply to all API routes
$middleware->api(append: [
\Teners\LaravelKeyCase\Http\Middleware\TransformResponseMiddleware::class,
\Teners\LaravelKeyCase\Http\Middleware\TransformRequestMiddleware::class,
]);
// Or register aliases for individual routes
$middleware->alias([
'transform-request' => \Teners\LaravelKeyCase\Http\Middleware\TransformRequestMiddleware::class,
'transform-response' => \Teners\LaravelKeyCase\Http\Middleware\TransformResponseMiddleware::class,
]);
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();Add to app/Http/Kernel.php:
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
protected $middlewareGroups = [
'web' => [
// ... other middleware
],
'api' => [
// ... other middleware
\Teners\LaravelKeyCase\Http\Middleware\TransformResponseMiddleware::class,
\Teners\LaravelKeyCase\Http\Middleware\TransformRequestMiddleware::class,
],
];
// For individual route usage
protected $middlewareAliases = [
// ... other aliases
'transform-request' => \Teners\LaravelKeyCase\Http\Middleware\TransformRequestMiddleware::class,
'transform-response' => \Teners\LaravelKeyCase\Http\Middleware\TransformResponseMiddleware::class,
];
}The package works with zero configuration, but you can customize it by publishing the config file:
<?php
return [
/**
* Response Key Case - Transform API response keys
* Options: camel, snake, kebab, studly, lower, upper, ucfirst, ucwords, singular, plural, slug, title
*/
'response_case' => env('RESPONSE_CASE', 'camel'),
/**
* Request Key Case - Transform incoming request keys
* Options: camel, snake, kebab, studly, lower, upper, ucfirst, ucwords, singular, plural, slug, title
*/
'request_case' => env('REQUEST_CASE', 'snake'),
/**
* Performance Settings
*/
'max_depth' => env('KEY_CASE_MAX_DEPTH', 10),
/**
* Route Exclusions
*/
'ignore' => [
'health-check',
],
'ignore_request' => [],
'ignore_response' => [],
];Frontend (JavaScript)
// Send camelCase data
const userData = {
firstName: "John",
lastName: "Doe",
emailAddress: "john@example.com",
};
fetch("/api/users", {
method: "POST",
body: JSON.stringify(userData),
});Backend (Laravel)
<?php
// Automatically received as snake_case
public function store(Request $request)
{
$validated = $request->validate([
'first_name' => 'required|string',
'last_name' => 'required|string',
'email_address' => 'required|email',
]);
User::create($validated);
return response()->json([
'user_id' => $user->id,
'created_at' => $user->created_at,
]);
// Automatically transformed to camelCase: {"userId": 1, "createdAt": "2024-01-01T12:00:00Z"}
}<?php
// Apply to specific routes
Route::middleware(['transform-response'])->group(function () {
Route::get('/users', [UserController::class, 'index']);
Route::post('/users', [UserController::class, 'store']);
});
// Skip transformation for specific routes
Route::get('/legacy-api/data', function () {
// This route will be ignored
});| Case Type | Example | Use Case |
|---|---|---|
camel |
firstName |
JavaScript, JSON APIs |
snake |
first_name |
PHP, Database columns |
kebab |
first-name |
URLs, CSS classes |
studly |
FirstName |
Class names |
lower |
firstname |
Simple keys |
upper |
FIRSTNAME |
Constants |
ucfirst |
Firstname |
Sentences |
ucwords |
First Name |
Titles |
singular |
user (from users) |
Model names |
plural |
users (from user) |
Collections |
slug |
first-name |
URLs |
title |
First Name |
Display text |
- Non-blocking - errors won't break your requests
Run the test suite:
# Run all tests
composer testContributions are welcome via Pull Requests on Github.
- Please document any change you made as neccesary in the README.md.
- Follow PSR-12 coding standards
- Write tests for new features
- Update documentation for any changes
- Make one pull request per feature/fix
- Ensure all tests pass
Please report any issue you encounter in using the package through the Github Issues tab.
When reporting issues, please include:
- Laravel version
- PHP version
- Package version
- Code example
- Error messages
To run tests, use:
composer testContributors list will be added here
The MIT License (MIT). Please see License File for more information.
Made with β€οΈ by Teners - if this package helped you β Star us on GitHub