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-Typeheader. - Representing and normalizing query parameters for the
GETrequest method. - Representing
form,json,xmlrequest body for thePOST,PUT,PATCHrequest methods. - Defining supported formats and throwing exception if the request format is unsupported.
- Dispatching the finish event when request data is ready.
Run the following command using Composer:
composer require bilyiv/request-data-bundleThe default configuration is the following:
request_data:
prefix: App\RequestDatanamespace App\RequestData;
class PostRequestData implements FormatSupportableInterface
{
public const DEFAULT_AUTHOR = 'none';
/**
* @var string
*/
public $title;
/**
* @var string
*/
public $author = self::DEFAULT_AUTHOR;
/**
* {@inheritdoc}
*/
public static function getSupportedFormats(): array
{
return [Formats::FORM, Formats::JSON, Formats::XML];
}
}namespace App\Controller;
class PostController extends AbstractController
{
/**
* @Route("/", name="action")
*/
public function action(PostRequestData $data)
{
return new JsonResponse($data);
}
}All the following requests will return the same json response:
{
"title": "Hamlet",
"author": "William Shakespeare"
}GET request:
curl -X GET 'https://example.com?title=Hamlet&author=William+Shakespeare'POST form request:
curl -X POST 'https://example.com' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'title=Hamlet&author=William+Shakespeare'POST json request:
curl -X POST 'https://example.com' \
-H 'Content-Type: application/json' \
-d '{"title":"Hamlet","author":"William Shakespeare"}'POST xml request:
curl -X POST 'https://example.com' \
-H 'Content-Type: application/xml' \
-d '<post><title>Hamlet</title><author>William Shakespeare</author></post>'POST csv request throws an exception because of unsupported format:
curl -X POST 'https://example.com' \
-H 'Content-Type: application/csv' \
-d 'Hamlet,William Shakespeare'This bundle is released under the MIT license. See the included LICENSE file for more information.