Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony route parameter requirement restriction

Tags:

symfony1

How do I enforce a requirement that a paramater in a route be a string?

Given the route

my_foobar_route: url: /example/routing/:s1/:id requirements: { id: \d+ }

Can anyone remind me of how to force param s1 to be a string?

like image 355
morpheous Avatar asked Jun 19 '10 01:06

morpheous


1 Answers

If you don't care what the string contains or if you don't know beforehand what it will contain, try the following:

my_foobar_route:
  url: /example/routing/:s1/:id
  requirements:
    id: \d+
    s1: "[^/]+"

This will allow all characters except the '/' character which is used as a separator for the parameters. With the expression

my_foobar_route:
  url: /example/routing/:s1/:id
  requirements:
    id: \d+
    s1: "[^/]{3,}"

you could force the string to be at least three characters long.

Don't forget to put Regexes with square brackets in quotes! If you forget them, the YAML parser for the routes will interpret them as an array expression.

like image 114
chiborg Avatar answered Sep 21 '22 08:09

chiborg