[英]zend framework get a variable from the controller to view
I'm new to zend and OOP in general. 我一般都是zend和OOP的新手。 I have a indexAction with a variable that I need to pass to the view.
我有一个indexAction,我需要传递给视图的变量。 I've declared the variable as public and I thought I could then get the variable in the view using
$this->variable
but it doesn't work. 我已经将变量声明为public并且我认为我可以使用
$this->variable
在视图中获取变量,但它不起作用。
How do I pass a variable from indexAction to the view? 如何将indexAction中的变量传递给视图?
Within indexAction, you need to assign it to the view instance. 在indexAction中,您需要将其分配给视图实例。 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 我更喜欢控制器中的assign方法,因为它可以很好地将多个变量添加到视图中
$this->view->assign('firstname', 'Peter')
->assign('lastname', 'Miller');
and in the view you can use the short open tag to echo
things. 在视图中,您可以使用短开标记来
echo
事物。 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
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.