Symfony: pass parameter between actions (with a redirect)

不打扰是莪最后的温柔 提交于 2019-12-30 18:42:06

问题


I am redirecting from one action (executeProcess) to another (executeIndex). I want to be able to pass a parameter/variable along, without using GET (e.g. $this->redirect('index', array('example'=>'true')))

Is there a way I can directly pass parameters without having it directly displayed in the URL? (e.g. POST). thanks.


回答1:


Why dont you use sessions to store values before redirecting, and then getting them on the other action after you redirected? like:

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
  }
}



回答2:


The best way of passing a variable between two Actions is by using 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');
}

To use the variable in Twig template use this --

{% for flashVar in app.session.flashbag.get('baz') %}
    {{ flashVar }}
{% endfor %}



回答3:


Another solution that does not redirect the browser

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');

  }
}

`



来源:https://stackoverflow.com/questions/5229494/symfony-pass-parameter-between-actions-with-a-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!