PHP code example of crtl / request-dto-resolver-bundle
1. Go to this page and download the library: Download crtl/request-dto-resolver-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
crtl / request-dto-resolver-bundle example snippets
namespace App\DTO;
use Crtl\RequestDTOResolverBundle\Attribute\BodyParam;
use Crtl\RequestDTOResolverBundle\Attribute\FileParam;
use Crtl\RequestDTOResolverBundle\Attribute\HeaderParam;
use Crtl\RequestDTOResolverBundle\Attribute\QueryParam;
use Crtl\RequestDTOResolverBundle\Attribute\RouteParam;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints as Assert;
#[RequestDTO]
class ExampleDTO
{
// Matches someParam in request body
#[BodyParam, Assert\NotBlank]
public ?string $someParam;
// Matches file in uploaded files
#[FileParam, Assert\NotNull]
public mixed $file;
// Matches Content-Type header in headers
#[HeaderParam("Content-Type"), Assert\NotBlank]
public string $contentType;
// Pass string to param if property does not match param name.
// Matches queryParamName in query params
#[QueryParam("queryParamName"), Assert\NotBlank]
public string $query;
// Matches id
#[RouteParam, Assert\NotBlank]
public string $id;
// Nested DTOs are supported for BodyParam and QueryParam
#[BodyParam("nested"), Assert\Valid]
public ?NestedRequestDTO $nestedBodyDto;
#[QueryParam("nested")]
public ?NestedRequestDTO $nestedQueryParamDto;
// Optionally implement constructor which accepts request object
public function __construct(Request $request) {
}
}
namespace App\Controller;
use App\DTO\ExampleDTO;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ExampleController extends AbstractController
{
#[Route("/example", name: "example")]
public function exampleAction(ExampleDTO $data): Response
{
// $data is an instance of ExampleDTO with validated request data
return new Response("DTO received and validated successfully!");
}
}
namespace App\EventListener;
use Crtl\RequestDTOResolverBundle\Exception\RequestValidationException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
class RequestValidationExceptionListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
KernelEvents::EXCEPTION => "onKernelException",
];
}
public function onKernelException(ExceptionEvent $event)
{
$exception = $event->getThrowable();
if ($exception instanceof RequestValidationException) {
$response = new JsonResponse([
"error" => "Validation failed",
"details" => $exception->getViolations(),
], JsonResponse::HTTP_BAD_REQUEST);
$event->setResponse($response);
}
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.