symfonyroutesrequirements

Symfony route length restriction


Does a route length requirement exist? For example: I found this on Stacko.:

/**
 * @Route("/{shortUrl}", requirements={„shortUrl" : "[a-zA-Z0-9]{8}"})
 * @method({"GET"})
 */

I hoped that the {8} restricts it to only be called when the "shortUrl" is exactly 8 chars long but that does not work.

For your understanding:
I built a URL-shortener and now I'm facing this issue:
The route above gets called when the user enters the URL-alias my site created for him and redirects him, this works as expected. This alias is formed from lower and upper case letters and digits (randomly).
But when he wants to view his profile (localhost/profile) the route above is called instead of the profile route, although the "profile" part is only 7 chars long.


Solution

  • Try this

    /**
     * @Route("/{shortUrl}", requirements={"shortUrl" : "^[a-zA-Z0-9]{8}$"})
     * @method({"GET"})
     */
    

    You need to use the regex symbols ^ and $ to say "begin with" and "end with". Also in your regular expression pattern your forgot the minus symbol between A and Z for UpperCase matching.