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

how set global setVars

Hi, I have a problem with Phalcon, I would like set global setVars from all the pages in my system, and this global variables join with the local variable in the method, see example:

class BaseController{
    function initialize(){
        $this->view->setvars(array("var1 => "aaaa", "var2" => "bbbb"));
    }
}

class IndexController{
    function indexAction(){
        $this->view->setVars(array("var3" => "ccccccc"));

        //In this page, i would like that this view see the variable present on the previous BaseController and the local variable
    }

    function anotherAction(){
        $this->view->setVars(array("var4" => "ddddddd"));

        //In this page, i would like that this view see the variable present on the previous BaseController and the local variable
    }
}

Is this possible? Thanks



13.7k
Accepted
answer
edited Nov '14

I have the following in services.php:

$di->set('view', function () use ($config, $di) {
$view->core = new \stdClass;
});

In my BaseController I have the following:

public function afterExecuteRoute(\Phalcon\Mvc\Dispatcher $dispatcher) {
    $this->view->core->title = $this->di->getTitle()->getTitle();
}

In my view I can access the "global" vars by: {{ core.title }}