How might one use the url_for or link_to helpers to create a link to the same page passing along all GET parameters?
I have a search page which accepts multiple parameters as filters to hide certain results. For example, search.html?query=abc&people=1&groups=0 would search for all items containing ‘abc’ that aren’t groups. I would therefore like a link which toggles these filters, passing the rest of the parameters unchanged to the current page. In the above example, links would be created to search.html?query=abc&people=1&groups=1 and search.html?query=abc&people=0&groups=0.
I am wondering if there is any way to specify ‘this’ as a route, such that if the route or page changed, the code wouldn’t have to.
Moreover, how can one pass all parameters to the helper? $sf_params can be used to access all the parameters, and the ‘query_string’ property can be passed to the helper, but is there any method of combining the two short of creating the string manually?
GET parameters can be passed to a
link_tomethod as an array in the third argument, orurl_forin the second. This is illustrated as follows:This is hidden in the source code. Here,
$paramsis the list of GET parameters, and$optionsHTML attributes to add to the tag.The
$sf_paramsfunction can be converted to a suitable array for passing withgetAll(). See the documentation here. Note that this is different from previous versions of Symfony. Also be aware you might have to callgetRawValue()from within a template:$sf_context->getInstance()->getRouting()->getCurrentRouteName()can then be passed as $routeName to get the current route. Thanks to Tom for this