alexlisenkov / uuid-argument-resolver-bundle
ramsey/uuid argument resolver bundle for Symfony
Installs: 27
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^7.3 || ^8.0
- nyholm/psr7: ^1.3
- ramsey/uuid: ^4.1
- symfony/config: ^5.2
- symfony/dependency-injection: ^5.2
- symfony/http-kernel: ^5.2
- symfony/psr-http-message-bridge: ^2.0
Requires (Dev)
- phpunit/phpunit: ^9.5
README
ramsey/uuid argument resolver bundle for Symfony
Install
composer require alexlisenkov/uuid-argument-resolver-bundle
Usage
namespace App\Controllers; use Psr\Http\Message\ResponseInterface; use Ramsey\Uuid\UuidInterface; class ResourceController /** * @Route("/{uuid}", name="show_resource", methods="GET") */ public function show(UuidInterface $resourceUuid, ResourceRepository $resourceRepository): ResponseInterface { $resource = $resourceRepository->findOneByUuid($resourceUuid); if ($resource === null) { return new ResourceNotFoundResponse(); } return new ResourceResponse($resource); }
Handling invalid uuid
By default, it will respond with 400 Bad Request
with body Invalid UUID
. But you can configure this by creating a service.
Custom response
Create a factory that creates a Psr\Http\Message\ResponseInterface
.
namespace App\Factory; use Psr\Http\Message\ResponseInterface; use Symfony\Component\HttpFoundation\Response; class InvalidUuidResponseFactory { public static function create(): ResponseInterface { return new Response(400, [], 'Invalid UUID'); } }
Override the alexlisenkov.uuid_argument_resolver_bundle.uuid_invalid_response
service with your factory.
alexlisenkov.uuid_argument_resolver_bundle.uuid_invalid_response: class: '@Psr\Http\Message\ResponseInterface' factory: [ 'App\Factory\InvalidUuidResponseFactory', create ]
Now an invalid UUID will return your response.
Testing
composer test
Contributing
Contributions are welcome.