passing variable from one controller to another

I want to pass a variable from one controller to another which should be eventually passed to the view. Unfortunately using return $this->forward('controllerB/actionB'); from controllerA where controllerB/actionB has a variable that needs to be passed to the view is not passed.

What is the best way to route variables from controllers to controller and finally to view ?



64.2k

Hi, i am using the dispatcher and forward

$this->dispatcher->forward(
    array(
        "action" => "search",
        "params" => array(1, 2, 3)
    )
);

docs : https://docs.phalconphp.com/en/latest/reference/dispatching.html#forwarding-to-other-actions

about views, i am using

//from the cotroller

$this->view->setVars([
    'name' => 'koko'
    'age' => 16
]);

//inside the view

echo $this->view->name; //prints koko


34.0k
return $this->dispatcher->forward([
        "controller" => "controllerB"
        "action" => "actionB",
        "params" => [
            "param1",
            "param2"
        ]
]);
// Get params in controllerB / actionB
$params = $this->dispather->getParam("param1"); // use getParams() to get all parameters