Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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



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