Twig - 动态替换GET参数的值

chr*_*_so 6 parameters request symfony twig

有没有办法从树枝上替换GET参数值?

例如,我在这个地址有一个页面:

http://localhost/app_dev.php/test/?param1=40&sort=name
Run Code Online (Sandbox Code Playgroud)

在我的树枝上,我想建立3个像这样的链接:

http://localhost/app_dev.php/test/?param1=40&sort=name 
http://localhost/app_dev.php/test/?param1=40&sort=address 
http://localhost/app_dev.php/test/?param1=40&sort=code 
Run Code Online (Sandbox Code Playgroud)

现在我在URL的末尾再次添加了"&sort"参数,但这个解决方案实际上是一个"补丁"而且很糟糕!

<a href="{{app.request.requesturi}}&sort=address">address</a>
Run Code Online (Sandbox Code Playgroud)

在这个例子中我只有2个参数,但实际上我有大约6个参数,因为通过提交a获得了生成它的链接.

ins*_*ere 27

这应该可以解决您的问题:

{{ path(app.request.attributes.get('_route'),
   app.request.query.all|merge({'sort': 'address'})) }}
Run Code Online (Sandbox Code Playgroud)

它获取当前路由和所有查询参数,这些参数在添加之前与您要更新的路径合并.