我从一个动作(executeProcess)重定向到另一个动作(executeIndex)。我希望能够传递参数/变量,而不使用GET(例如$this->redirect('index', array('example'=>'true'))
)
有没有办法直接传递参数而不直接在URL中显示? (例如POST)。谢谢。
答案 0 :(得分:6)
为什么在重定向之前不使用会话来存储值,然后在重定向后让它们执行其他操作?像:
class ActionClass1 extendes sfActions
{
public function executeAction1(sfWebRequest $request)
{
[..]//Do Some stuff
$this->getUser()->setAttribute('var',$variable1);
$this->redirect('another_module/action2');
}
}
class ActionClass2 extends sfActions
{
public function executeAction2(sfWebRequest $request)
{
$this->other_action_var = $this->getUser()->getAttribute('var');
//Now we need to remove it so this dont create any inconsistence
//regarding user navigation
$this->getUser()->getAttributeHolder()->remove('var');
[...]//Do some stuff
}
}
答案 1 :(得分:5)
在两个操作之间传递变量的最佳方法是使用FlashBag
public function fooAction() {
$this->get('session')->getFlashBag()->add('baz', 'Some variable');
return $this->redirect(/*Your Redirect Code to barAction*/);
}
public function barAction() {
$baz = $this->get('session')->getFlashBag()->get('baz');
}
要在Twig模板中使用该变量,请使用此 -
{% for flashVar in app.session.flashbag.get('baz') %}
{{ flashVar }}
{% endfor %}
答案 2 :(得分:2)
另一种不重定向浏览器的解决方案
class someActionClass extends sfActions{
function myExecute(){
$this->getRequest()->setParameter('myvar', 'myval');
$this->forward('mymodule', 'myaction')
}
}
//Here are your actions in another module
class someActionClass2 extends sfActions{
function myExecute2(){
$myvar = $this->getRequest()->getParameter('myvar');
}
}
`