teners/laravel-key-case

Middleware for automatic case transformation of request and response data key in Laravel applications.

v1.1.2 2024-06-25 10:15 UTC

This package is auto-updated.

Last update: 2024-09-25 11:04:47 UTC


README

A Laravel package that transforms request and response data keys.

Latest Version on Packagist GitHub Tests Action Status issues stars GitHub license Total Downloads

Why?

It is a common convention to work with camel case in Javascript and most front-end technologies and snake case in PHP (Backend), this package makes that a breeze as you can work with your data columns just as you like in laravel and your responces or request data are automatically converted to any case.

Installation

Install Laravel Key Case, run the following command in your terminal:

composer require teners/laravel-key-case

Publish the package configuration file

php artisan vendor:publish --provider="Teners\LaravelKeyCase\LaravelKeyCaseServiceProvider" --tag="key-case-config"

You can then customize what case to use for each of the request and the response data.

Use the middlewares

This package includes two middlewares

  • TransformResponseMiddleware: Transforms response data keys.
  • TransformRequestMiddleware: Transforms request data keys.

You can register their aliases (or use them dirrectly on the api route group if you are using them through the api) for easy reference elsewhere in your app:

In Laravel 11 open /bootstrap/app.php and register them there:

    ->withMiddleware(function (Middleware $middleware) {
        $middleware->api(append: [
            \Teners\LaravelKeyCase\Http\Middleware\TransformResponseMiddleware:class,
        ]);
    })

    // Or to use on individual routes
    
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->alias([
            'convert-response-key' => \Teners\LaravelKeyCase\Http\Middleware\TransformResponseMiddleware:class,
        ]);
    })

In Laravel 9 and 10 you can add them in /app/Http/Kernel.php:

    protected $middlewareGroups = [
        'web' => [
            ...
        ],

        'api' => [
            ...
            TransformResponseMiddleware::class,
        ],
    ];

    // Or to use on individual routes

    // Laravel 9 uses $routeMiddleware = [
    //protected $routeMiddleware = [
    // Laravel 10+ uses $middlewareAliases = [
    protected $middlewareAliases = [
        // ...
        'convert-response-key' => \Teners\LaravelKeyCase\Http\Middleware\TransformResponseMiddleware:class,
    ];

Ignoring Routes

You can specify routes to be ignored for request and response transformation in the configuration file:

return [
    // Other configuration options...

    'ignore' => [
        // Common routes to ignore for both request and response transformation
    ],

    'ignoreRequest' => [
        // List of routes to ignore for request transformation
    ],

    'ignoreResponse' => [
        // List of routes to ignore for response transformation
    ],
];

Route listed in the ignore will not be transformed for bothe the resquest and response, while those in the ignoreRequest will not be transformed for only the request, and those in ignoreResponse will be ignored for the response transformation.

Contributions

Contributions are welcome via Pull Requests on Github.

  • Please document any change you made as neccesary in the README.md.
  • Pleas make only one pull request per feature/fix.

Issues

Please report any issue you encounter in using the package through the Github Issues tab.

Testing

To run tests, use:

composer test

Credits

Contributors

Contributors list will be added here

License

The MIT License (MIT). Please see License File for more information.