RequestInput bundle provides auto-transform request data into DTO input objects
- Request data supported:
json
,xml
andform
- Resolve inputs arguments for controllers actions
Require the bundle with composer:
composer require sfmok/request-input-bundle
- Create DTO input and implements
Sfmok\RequestInput\InputInterface
use Sfmok\RequestInput\InputInterface;
class PostInput implements InputInterface
{
/**
* @Assert\NotBlank()
*/
private string $title;
/**
* @Assert\NotBlank()
*/
private string $content;
/**
* @Assert\NotBlank()
*/
private array $tags = [];
/**
* @SerializedName('author')
* @Assert\NotBlank()
*/
private string $name;
# getters and setters or make properties public
}
- Use DTO input in your controller action as an argument:
class PostController
{
/**
* @Route("/posts", methods={"POST"})
*/
public function create(PostInput $input): Response
{
# dump input
dd($input);
# set entity data and store
$post = (new Post())
->setTitle($input->getTitle())
->setContent($input->getContent())
->setTags($input->getTags())
->setName($input->getName())
;
$em->persist($post);
$em->flush();
...
}
}
- Response header
Content-Type: application/problem+json; charset=utf-8
- Response body
{
"type": "https://symfony.com/errors/validation",
"title": "Validation Failed",
"detail": "title: This value should not be blank.",
"violations": [
{
"propertyPath": "title",
"title": "This value should not be blank.",
"parameters": {
"{{ value }}": "\"\""
},
"type": "urn:uuid:c1051bb4-d103-4f74-8988-acbcafc7fdc3"
}
]
}
- In case you want to serve a specific format for your inputs:
# config/packages/request_input.yaml
request_input:
enabled: true # default value true
formats: ['json'] # default value ['json', 'xml', 'form']
with above configuration RequestInputBundle will transform JSON request data only.