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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With