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 can I pass variables to main view

Hi How can I pass variables to main view, not just the view for controller.

in fact I want to share a piece of data across all views. how can I do that in phalcon



8.8k
Accepted
answer
edited Jun '14
  1. Use a base controller all your controllers inherit from
        class IndexController extends ControllerBase {

          public function initialize() {
              Tag::setTitle('Welcome');
              parent::initialize();
          }

          public function indexAction() {

          }
}
  1. define the initialize method in the base controller and define the vars
class ControllerBase extends \Phalcon\Mvc\Controller {

            public function initialize(){
                $this->view->setVar("globalBlaa","Helllooo");
            }
}
  1. show it in any view
        {{globalBlaa }}


16.3k

improve:

$this->view->globalBlaa = "Helllooo";
$this->view->setVars(array(
    'globalBlaa' => "Helllooo"
));


8.1k
edited Jun '14

@sumeko: are you doing the same in these two statements?