繁体   English   中英

如何在Zend Framework 2中传递和显示变量

[英]How to pass and display variable in Zend Framework 2

我想学习Zend Framework 2,我已经在机器上安装了它,到达了欢迎页面,那很好,所以当我看它的结构时,它在CodeIgniter或Laravel中有很大的不同,我的问题是我想显示一些文本或我的index.phtml字符串。 在我的IndexController.php我得到了这个。

public function indexAction() {
    $sample = 'sample string'; // this is what I want to pass in my index.phtml
    return new ViewModel();
}
  1. 如何将变量传递到index.phtml
  2. 如何在index.phtml显示它?

例如,在CodeIgniter中看起来像这样。

//Controller.php
public function indexAction() {
    $data = 'Sample data'; // this is sample string that will display in my index.php
    return view('home',$data);
}
//View
<?php
   echo $data; // it will print the string in my $data(variable); 
?>

我是Zend Framework 2的新手,对此框架我一无所知。 帮帮我
预先感谢您帮助莫解决我的问题。

在Zend 2中,您可以将要在视图中使用的任何变量传递给ViewModel或返回一个数组:

public function indexAction() {
    $sample = 'sample string'; // this is what I want to pass in my index.phtml
    return new ViewModel(array(
        "sample" => $sample
    ));
}

要么:

public function indexAction() {
    $sample = 'sample string'; // this is what I want to pass in my index.phtml
    return array(
        "sample" => $sample
    );
}

然后,您可以像这样在index.phtml访问$sample

<?php echo $this->sample ?>

数组键将始终是视图的属性名称。

有关更多信息,请参见:

http://framework.zend.com/manual/current/en/modules/zend.view.quick-start.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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