zend framework get a variable from the controller to view
I'm new to zend and OOP in general. I have a indexAction with a variable that I need to pass to the view. I've declared the variable as public and I thoug开发者_StackOverflow社区ht I could then get the variable in the view using $this->variable
but it doesn't work.
How do I pass a variable from indexAction to the view?
Within indexAction, you need to assign it to the view instance. simply do:
$this->view->something = "foo";
and in your view:
<?php echo $this->something ?>
I prefer the assign method in the controller because it lets me nicely add multiple variables to the view
$this->view->assign('firstname', 'Peter')
->assign('lastname', 'Miller');
and in the view you can use the short open tag to echo
things. And never forget to quote things.
<body>
Firstname: <?= $this->escape($this->firstname); ?><br />
Lastname: <?= $this->escape($this->lastname); ?>
</body>
$this->view->assign(array(
'var1' => $value1,
'var2' => $value2
));
精彩评论