简体   繁体   中英

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 thought 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
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM