Represents request data in a structured and useful way.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
DependencyInjection
Event rename deserialized event to finish Feb 2, 2019
EventListener remove extra brackets Feb 10, 2019
Extractor implement xml format supporting Feb 10, 2019
Mapper check if propery is writable Feb 8, 2019
Resources/config remove request data normalizer, original data trait Feb 9, 2019
Tests
TypeConverter remove extra brackets Feb 10, 2019
.gitignore create gitignore Jan 3, 2019
Events.php
Formats.php
LICENSE Create LICENSE Feb 2, 2019
README.md add xml to readme, format curl code Feb 10, 2019
RequestDataBundle.php remove src directory Jan 20, 2019
composer.json Fix PHP version in composer.json Feb 18, 2019

README.md

Request data bundle

This bundle allows you to represent request data in a structured and useful way by creating request data classes.

Features:

  • Detecting how to extract data depends on request method and Content-Type header.
  • Representing and normalizing query parameters for the GET request method.
  • Representing form, json, xml request body for the POST, PUT, PATCH request methods.
  • Dispatching the finish event when request data is ready.

Installation

Run the following command using Composer:

composer require bilyiv/request-data-bundle

Configuration

The default configuration is the following:

request_data:
    prefix: App\RequestData

Usage

Create a request data class

namespace App\RequestData;

class PostRequestData
{
    public const DEFAULT_AUTHOR = 'none';

    /**
     * @var string
     */
    public $title;

    /**
     * @var string
     */
    public $author = self::DEFAULT_AUTHOR;
}

Use it in your controller

namespace App\Controller;

class PostController extends AbstractController
{
    /**
     * @Route("/", name="action")
     */
    public function action(PostRequestData $data)
    {
       return new JsonResponse($data);
    }
}

Make requests

All the following requests will return the same response {"title":"It works","author":"Vlad"}:

curl -X GET 'http://localhost:8000?title=It+works&author=Vlad'
curl -X POST 'http://localhost:8000' \
     -H 'Content-Type: application/x-www-form-urlencoded' \
     -d 'title=It+works&author=Vlad'
curl -X POST 'http://localhost:8000' \
     -H 'Content-Type: application/json' \
     -d '{"title":"It works","author":"Vlad"}'
curl -X POST 'http://localhost:8000' \
     -H 'Content-Type: application/xml' \
     -d '<post><title>It works</title><author>Vlad</author></post>'

License

This bundle is released under the MIT license. See the included LICENSE file for more information.