6
votes

What I'd like to do is take the route for the current action along with any and all of the route and query string parameters, and change a single query string parameter to something else. If the parameter is set in the current request, I'd like it replaced. If not, I'd like it added. Is there a helper for something like this, or do I need to write my own?

Thanks!

[edit:] Man, I was unclear on what I actually want to do. I want to generate the URL for "this page", but change one of the variables. Imagine the page I'm on is a search results page that says "no results, but try one of these", followed by a bunch of links. The links would contain all the search parameters, except the one I would change per-link.

5

5 Answers

5
votes

Edit:

Ok I got a better idea now what you want. I don't know whether it is the best way but you could try this (in the view):

url_for('foo', 
        array_merge($sf_request->getParameterHolder()->getAll(), 
                    array('bar' => 'barz'))
)

If you use this very often I suggest to create your own helper that works like a wrapper for url_for.

Or if you only want a subset of the request parameters, do this:

url_for('foo', 
         array_merge($sf_request->extractParameters(array('parameter1', 'parameter3')),
                     array('bar' => 'barz'))
)

(I formated the code this way for better readability)


Original Answer:

I don't know where you want to change a parameter (in the controller?), but if you have access to the current sfRequest object, this should do it:

$request->setParameter('key', 'value')

You can obtain the request object by either defining your action this way:

public function executeIndex($request) {
     // ...
}

or this

public function executeIndex() {
     $request = $this->getRequest();
}
2
votes

For symfony 1.4 I used:

$current_uri = sfContext::getInstance()->getRouting()->getCurrentInternalUri();
$uri_params = $sf_request->getParameterHolder()->getAll();
$url = url_for($current_uri.'?'.http_build_query(array_merge($uri_params, array('page' => $page))));
echo link_to($page, $url);
1
votes

Felix's suggestion is good, however, it'd require you to hard core the "current route"..

You can get the name of the current route by using:

sfRouting::getInstance()->getCurrentRouteName()

and you can plug that directly in url_for, like so:

url_for(sfRouting::getInstance()->getCurrentRouteName(), 
         array_merge($sf_request->extractParameters(array('parameter1', 'parameter3')),
                     array('bar' => 'barz'))
)

Hope that helps.

0
votes

With the same concept than Erq, and thanks to his code, I have made the same with some small changes, since my URL needs to convert some characters. Its generic though and should work with most forms, in order to save the parameters the user has chosen to search for.

public function executeSaveFormQuery(sfWebRequest $request)
{
$sURLServer = "http://";
$sURLInternalUri = "";
$page = "";
$sURLInternalUri = sfContext::getInstance()->getRouting()->getCurrentInternalUri();
$suri_params = $request->getParameterHolder()->getAll();
$sParams = http_build_query(array_merge($suri_params));

$dpos = strpos($sURLInternalUri, "?");
$sURLConsulta[$dpos] = '/';
$sURL = substr($sURLInternalUri, $dpos);
$dpos = strpos($sURL, "=");
$sURL[$dpos] = '/';

$sURLFinal = $sURLServer . $sURL . '?' . $sParams;

//$this->redirect($this->module_name . '/new');
self::executeNew($request, $sURLFinal);
//echo "var_dump(sURLFinal): "; 
//var_dump($sURLFinal);
//echo "<br></br>";
//return sfView::NONE;
}

In executeNew, as easy as:

public function executeNew(sfWebRequest $request, $sURLQuery)
{
//$sURLQuery= "http://";
if ($sURLQuery!= "")
{
    $this->form = new sfGuardQueryForm();
    //echo "var_dump(sURLQuery)";
    //var_dump($sURLQuery);
    //echo "<br></br>";

    $this->form->setDefault('surl', $sURLQuery);
}
else
{
            $this->form = new sfGuardQueryForm();
}
}
-1
votes

echo $sf_context->getRequest()->getUri();