tags:

views:

38

answers:

2

I am trying to pass parameters from one action (foo) to another (foobar).

In action foo, I set the arguments thus:

$request->getParameterHolder()->set('arg1', 'alice');
$request->getParameterHolder()->set('arg2', 'bob');

In action foobar, I try to retrieve the params thus:

$arg1 = $request->getParameter('arg1');
$arg2 = $request->getParameter('arg2');

$this->forward404Unless($arg1 && $arg2); //always forwarded

Note: I am aware that I can save the params into the user session variable - but I dont want to do that. I want to pass them as parameters - any ideas how to get this to work?

+2  A: 

You can simply try this:

    $this->redirect('module/action2?'.
      http_build_query(array("arg1"=> "alice", "arg2"=>"bob")));
greg0ire
D'oh! - I had forgotten about http_build_query(). Why do I always like to make things more complicated ;) +1 for the simple, elegant soln.
morpheous
+1  A: 

greg0ire's answer sounds like it's what you are asking for but there are a couple of other approaches that might be worth looking at if passing query string parameters isn't a hard requirement.

You could use a forward if you want the foobar action to execute after foo. Unlike a redirect this will live in the same request cycle so you can pass variables without touching the session.

You don't say why you don't want to use the session but there is a halfway house in Symfony: flash attributes. These are stored in the session but are guaranteed not to live beyond the next request which may be a suitable compromise.

Colonel Sponsz
@sponz: I dont want to use flash attribute either ;) I should have explained, I am using a public API that expects a GET request with all the params attached to it. Your tip about forward explains why the snippet I posted sometimes work. Upon checking my code, I found the occasion on which it worked, I had done a forward. +1 for that
morpheous
I knew it wasn't ultimately the answer you were looking for but glad it helped. The different ways to pass data between actions in Symfony and their consequences can be a bit confusing. Mainly to get it to show up in the linked answers, here's one of my previous answers on the subject: http://stackoverflow.com/questions/2896274/symfony-trying-to-retrieve-a-variable-saved-using-sfcontextgetinstance/2896341#2896341
Colonel Sponsz