coderscantina/laravel-transform-requests

A form request wrapper for applying transformations to incoming requests

v1.0.1 2024-10-24 19:12 UTC

This package is auto-updated.

Last update: 2024-11-24 19:25:01 UTC


README

A form request wrapper for applying transformations to incoming requests

Features

  • Use TransformRequest to specify an array of key replacements
  • Use ApiRequest to transform all input from camelCase to snake_case

Getting started

  • Install this package

Install

Require this package with composer:

$ composer require coderscantina/laravel-transform-requests

Usage

Define a new ApiRequest:

<?php namespace App;
 
use Neon\Request\ApiRequest;
 
class TestApiRequest extends ApiRequest
{

}

Define a new TransformRequest:

  • Override the $transform field to define your transformations
  • To further customize the transformation override getTransform
<?php namespace App;
 
use Neon\Request\TransformRequest;
 
class TestTransformRequest extends TransformRequest
{
    protected $transform = [
        'foo_bar' => 'fooBar',
    ];

}

In your application, use the request as you would any other request:

curl -X POST -d '{"fooBar": "baz"}' https://localhost/
<?php

class TestController extends \Illuminate\Routing\Controller
{
    public function a(TestTransformRequest $request)
    {
        $request->get('foo_bar'); // 'baz'
        $request->all(); // -> ['foo_bar' => 'baz']
    }
    
    public function b(TestApiRequest $request)
    {
        $request->get('foo_bar'); // 'baz'
        $request->all(); // -> ['foo_bar' => 'baz']
    }
}

Testing

$ composer test