I have two routes :
// routing.yml
foo_list:
path: /foo
defaults: { _controller:MyBundle:Foo:index }
foo_view:
path: /foo/{id}
defaults:
_controller: AlfnooBundle:Exploitant:view
id : 1
requirements:
id: \d*
The first one is for a page which lists all the foos, the second one is the page which display a detailed view of foo n. {id}. In my Twig view, I generate the link to foo_view
like this :
{{path('foo_view', {'id': foo.id})}}
It's OK if id
is not 1
(the default value) : /foo/12345
. But if id
is 1
, the path()
method generates a wrong url, it ommits the id
param, and the generated route is : /foo
. Which is not foo_view
, but foo_list
. If I change the default value, the issue is for the default value.
{{path('foo_view', {'id': 1})}} => /foo => BAD ! I want /foo/1
{{path('foo_view', {'id': 123})}} => /foo/123 => GOOD
So how can I force Twig path()
methods to always add the param value, even if it is the default value ? I want to keep disctincts urls for :
/foo => foo_list
/foo/ => foo_view with id=1
/foo/12345 => foo_view with id=12345
Thank you.
You can't do this, at least not in this way. Symfony's router does not generate paths with trailing slashes.
The path generated by path('foo_view')
will always be /foo
and will never be /foo/
and since you've specified a default for id
the path will never be /foo/1
either, as you've seen.
There is a way to coerce trailing slashes but it's a but clumsy if you ask me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.