symfony/symfony — pull request #3965 by Tobion (2012-04-17 14:37:00+00:00)
Title: [RFC][Twig] added subpath function

The new function `subpath` merges the variables of the current route request with the ones you optionally passed to the function. On top of that you can also add the query params (disabled by default).
By default it will generate a path-relative URL once #3958 gets merged.

Example: We are at `/user-slug/article-slug/` (article_show) and want to generate the link to comments of the article (article_comments). At the moment we would have to always repeat the current variables like
`{{ path('article_comments', {'user': 'user-slug', 'article': 'article-slug'}) }}` and would for example get `/user-slug/article-slug/comments`.

With `subpath` the advantage is you don't need to specify the variables of the current request again.
So simply `{{ subpath('article_comments') }}` is enough for the relative URL `comments` in this case. But you can optionally overwrite some request variables with {{ subpath('article_comments', {'article': 'different-article-slug'} ) }} which would generate `../different-article-slug/comments` (i.e. same user, but different article).

Use cases: Every page that is hierarchically structured (almost all). Also generating links for the same page with a different locale for example.
Merging the query params can be used for filtering links, e.g. a facetted navigation page that filters items and you want to generate a link to add/overwrite filters based on the current filters.

Developer question: I guess injecting the request with `strict=false` is not the correct way. Do I need to inject the container or is there another way?

Ref: This implements the idea suggested in #3908.