6

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.

Rytmis
  • 31,467
  • 8
  • 60
  • 69

5 Answers5

5

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();
}
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Preferably, I'd like to do it in the view, using something like url_for('foo', array('bar' => 'baz')), but so that the current variables are maintained and I don't have to explicitly set them. – Rytmis Jan 05 '10 at 14:23
  • 1
    If i recall i believe the request is exposed as `$sf_request` in the view. – prodigitalson Jan 06 '10 at 04:06
2

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);
Erq
  • 21
  • 1
1

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.

0x6A75616E
  • 4,696
  • 2
  • 33
  • 57
  • I was so focused on the parameter merging thing that I totally forgot about the current page. Good point. You should reformat your code though. – Felix Kling Jan 08 '10 at 02:50
  • Actually, I only need this in one place for now, so hard-coding the route name is not a problem. I had already come up with basically the same solution, although I didn't know about the parameter holder. Thanks to both of you for your great suggestions. :) – Rytmis Jan 08 '10 at 09:06
0

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();
}
}
xtrm
  • 966
  • 9
  • 22
-1

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