1

I have a route with a parameter {id} on which I want to apply a condition. Only if {id} contains more than 3 characters, this route should be available. I read about conditions in routes both in the documentation and in a similar question. What I tried:

/**
 * @Route("/{id}", name="some_route", condition="strlen(id) > 3")
 */

I also tried with requirements instead of condition and tried {id} instead of id. No luck. The error that is show is:

The function "strlen" does not exist around position 1 for expression strlen(id) > 3.

How to solve this issue?

1
  • Try with regex. Add requirements as [requirements={"id"="[0-9]{3}"}] Commented Jun 26, 2019 at 9:20

1 Answer 1

2

In this case, using regular expression is the best option. Take a look in the PHP documentation to find out more (Here is the link). And suppose the characters you are referring to are alphanumeric characters (I added the - and _).

<?php

// some code ...

/**
 * @Route("/{id}", name="some_route", requirements={"id"="[a-zA-Z0-9_-]{3,}"})
 */

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.