We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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 ?



85.5k

Hi, i am using the dispatcher and forward

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

docs : https://docs.phalcon.io/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


51.1k
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