定制 teners/laravel-key-case 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

teners/laravel-key-case

最新稳定版本:v1.2.2

Composer 安装命令:

composer require teners/laravel-key-case

包简介

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

README 文档

README

A high-performance Laravel package that automatically transforms request and response data keys between different naming conventions (camelCase ↔ snake_case, kebab-case, etc.).

Latest Version on Packagist GitHub Tests Action Status Issues Stars GitHub License Total Downloads

🎯 Why Laravel Key Case?

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.

📦 Installation

Install via Composer:

composer require teners/laravel-key-case

Publish Configuration (Optional)

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

Quick Start

Laravel 11

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();

Older laravel versions

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,
    ];
}

Configuration

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' => [],
];

📖 Usage Examples

Basic API Transformation

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"}
}

Route-Specific Control

<?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
});

Supported Case Types

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

Error Resilience

  • Non-blocking - errors won't break your requests

🧪 Testing

Run the test suite:

# Run all tests
composer test

Contributions

Contributions 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

Issues

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

Testing

To run tests, use:

composer test

Credits

Contributors list will be added here

License

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

Made with ❤️ by Teners - if this package helped you ⭐ Star us on GitHub

统计信息

  • 总下载量: 3.85k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-13