Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a path appending query string in Symfony2

Tags:

twig

symfony

Is there any facility to generate a path for a given route and arguments, appending the query string automatically? As a temporary workaround i'm using a self made macro:

{% macro path(route, args, with_query) %}
{% spaceless %}
    {% set with_query = with_query|default(false) and app.request.queryString %}
    {{ path(route, args) ~ (with_query ? '?' ~ app.request.queryString : '' ) }}
{% endspaceless %}
{% endmacro %}

Is there some native function in Symfony2/Twig for doing this?

like image 791
gremo Avatar asked Mar 20 '12 21:03

gremo


2 Answers

A nice thing with path Twig extension is that unknow parameters passed through the args array are automatically appended at the end of the URL as GET paramaters :

{{ path('route_id', {'routeParam':'foo', 'unknownParam':'bar'}) }}

will produce

/path/to/route/foo?unknownParam=bar

like image 156
AlterPHP Avatar answered Nov 18 '22 23:11

AlterPHP


As simple as :

{{ path('route_id', app.request.query.all) }}
like image 4
Jorj Avatar answered Nov 19 '22 00:11

Jorj