3

I'm working on a symfony 5 project.

I use ramsey/uuid.

My doctrine.yaml

 dbal:
     types:
         uuid: Ramsey\Uuid\Doctrine\UuidType

My route in my controller

/**
 * @Route(
 *     "/job/{id}",
 *     name="job_show",
 *     methods={"GET"}
 * )
 */

I would like to add the requirements to verify if the "id" parameter is a uuid.

I tried with several regex but none work :

Regex tried :

requirements={"id"="/^[a-f0-9]{8}\-[a-f0-9]{4}\-4[a-f0-9]{3}\-[a-f0-9]{4​}\-[a-f0-9]{12}$/"}
requirements={"id"="/^[a-f0-9\-]{36}$/"}

Every time I get this error:

No route found for "GET /job/dc5a945c-25a1-4760-bd69-970d94560cce"

I have watch severals similar questions like Searching for UUIDs in text with regex but none helped me.

Does anyone know where my error may come from or how to do it differently ?

BanjoSf4
  • 149
  • 1
  • 1
  • 13

1 Answers1

6

You can use:

requirements={"id"="[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"})

Or, as of Symfony 6.1, you can use predefined route requirements:

use Symfony\Component\Routing\Requirement\Requirement;

#[Route('/job/{id}',
    name: 'job_show',
    requirements: ['id' => Requirement::UUID_V6]
)]

Check the feature introduction on the Symfony blog.

COil
  • 7,201
  • 2
  • 50
  • 98