Werner
Werner

Reputation: 1847

Symfony: Multiple folders in route

I have a route like:

pattern: /{name}-id{id}/
defaults: { _controller: mycontroller:myfunc }
requirements:       
    name: "[a-z0-9-]+"
    id: "[0-9]+"

which works fine. Now I want to make the route work for multiple pathes, so that it's triggered every time the url ends with -id{id}/, like:

/{name}-id{id}/
/level1/{name}-id{id}/
/level1/level2/{name}-id{id}/
/level1/level2/level3/{name}-id{id}/

and so on. How should this be done without repeating the definition of the route endless times?

Upvotes: 2

Views: 249

Answers (1)

M Khalid Junaid
M Khalid Junaid

Reputation: 64476

As per the docs

By default, the Symfony Routing component requires that the parameters match the following regular expression: [^/]+. This means that all characters are allowed except /.

You must explicitly allow / to be part of your placeholder by specifying a more permissive regular expression for i

You can adjust your route as

pattern: /{path}/{name}-id{id}/
defaults: { _controller: mycontroller:myfunc }
requirements:       
    path: .+
    name: "[a-z0-9-]+"
    id: "[0-9]+"

Upvotes: 3

Related Questions