Symfony: pass parameter between actions (with a redirect)

后端 未结 3 1750
清酒与你
清酒与你 2021-01-18 07:35

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

3条回答
  •  无人及你
    2021-01-18 08:37

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

提交回复
热议问题