Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass variables to main view in phalconphp

Tags:

php

phalcon

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.

for instance, in Laravel, I can use:

View::share('variable', $value);

like image 444
Pars Avatar asked Dec 25 '22 07:12

Pars


1 Answers

With Phalcon you don't need any additional functions to pass data into the layout. All variables assigned in the action are available in the layout automatically:

Controller:

class IndexController extends \Phalcon\Mvc\Controller
{
    public function indexAction()
    {
        $this->view->bg    = 'green';
        $this->view->hello = 'world';
    }
}

Main layout:

<html>
<body style="background: <?php echo $bg ?>">

    <?php echo $this->getContent(); ?>

</body>
</html>

Action view:

<div>
    <?php echo $hello ?>
</div>

You will see text 'world' on the green background.

like image 138
Phantom Avatar answered Jan 09 '23 03:01

Phantom