coderscantina / laravel-transform-requests
A form request wrapper for applying transformations to incoming requests
Installs: 4 050
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- laravel/framework: ^7.0||^8.0||^9.0||^10.0||^11.0
Requires (Dev)
- phpunit/phpunit: ~8.0||~9.0
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