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 con开发者_JAVA百科taining '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_to
method as an array in the third argument, or url_for
in the second. This is illustrated as follows:
link_to('Text to display', 'routename', array('group' => 0, 'people' => 1), $link_attributes);
url_for('routename', array('group' => 0, 'people' => 1));
This is hidden in the source code. Here, $params
is the list of GET parameters, and $options
HTML attributes to add to the tag.
function link_to2($name, $routeName, $params, $options = array())
function url_for2($routeName, $params = array(), $absolute = false)
The $sf_params
function can be converted to a suitable array for passing with getAll()
. See the documentation here. Note that this is different from previous versions of Symfony. Also be aware you might have to call getRawValue()
from within a template:
$params = $sf_params->getRawValue()->getAll();
$sf_context->getInstance()->getRouting()->getCurrentRouteName()
can then be passed as $routeName to get the current route. Thanks to Tom for this
put vars in hidden form data types to pass them on
<form action="page.php" method="GET">
<input name="people" type="hidden" value="<?php echo htmlspecialchars($_GET['people'], ENT_QUOTES); ?>">
<input name="groups" type="hidden" value="<?php echo htmlspecialchars($_GET['groups'], ENT_QUOTES); ?>">
<input name="query" type="text" value="some_data">
<input name="send" type="submit" value="send">
</form>
精彩评论