Deserializes a request to an entity.
Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.
Open a command console, enter your project directory and execute:
$ composer require halloverden/symfony-request-entity-bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require halloverden/request-entity-bundle
Then, enable the bundle by adding it to the list of registered bundles
in the config/bundles.php
file of your project:
// config/bundles.php
return [
// ...
HalloVerden\RequestEntityBundle\HalloVerdenRequestEntityBundle::class => ['all' => true],
];
-
Create a class that extends
AbstractRequestEntity
. example:<?php namespace App\Entity\Requests; use App\Entity\Yoo; use HalloVerden\RequestEntityBundle\Requests\AbstractRequestEntity; use JMS\Serializer\Annotation as Serializer; use Symfony\Component\Validator\Constraints as Assert; /** * Class TestRequest * * @package App\Entity\Requests * * @Serializer\ExclusionPolicy("ALL") */ class TestRequest extends AbstractRequestEntity { /** * @var array|null * * @Serializer\SerializedName("test") * @Serializer\Type(name="array") * @Serializer\Expose() */ private $test; /** * @var Yoo * * @Serializer\SerializedName("yoo") * @Serializer\Type(name="App\Entity\Yoo") * @Serializer\Expose() */ private $yoo; /** * @return array|null */ public function getTest(): ?array { return $this->test; } /** * @inheritDoc */ protected static function getRequestDataValidationFields(): array { return [ 'test' => [new Assert\Type(['type' => 'array']), new Assert\Count(['min' => 1])], 'yoo' => new Assert\Collection([ 'fields' => [ 'message' => [new Assert\Type(['type' => 'string']), new Assert\NotBlank()] ], 'allowMissingFields' => static::allowMissingFields(), 'allowExtraFields' => static::allowExtraFields() ]) ]; } }
Override the
getRequestDataValidationFields
method to validate the request data. These validation rules must match the incoming request which is an array (i.e. after decoding a json request) -
In your controller inject this request class using the paramconverter. Example:
<?php namespace App\Controller; use App\Entity\Requests\TestRequest; use App\Response\TestResponse; use HalloVerden\ResponseEntityBundle\Controller\AbstractResponseEntityController; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Annotation\Route; /** * Class Test2Controller * * @package App\Controller * * @Route("/test2", methods={"POST"}, name="testpost") */ class Test2Controller extends AbstractResponseEntityController { /** * @ParamConverter("testRequest", converter="HalloVerden\RequestEntityBundle\ParamConverter\RequestEntityConverter", class="App\Entity\Requests\TestRequest") * * @param TestRequest $testRequest * * @return JsonResponse */ public function __invoke(TestRequest $testRequest): JsonResponse { return $this->createJsonResponse(new TestResponse($testRequest->getTest())); } }