如何在 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();
}
- 如何将变量传递到我的
index.phtml
中?
- 如何在我的
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 的新手,我对这个框架一无所知。帮帮我。
预先感谢您帮助 mo 解决我的问题。
在 Zend 2 中,您可以将要在视图中使用的任何变量传递给 ViewModel
或 return 数组:
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
我想学习 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();
}
- 如何将变量传递到我的
index.phtml
中? - 如何在我的
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 的新手,我对这个框架一无所知。帮帮我。
预先感谢您帮助 mo 解决我的问题。
在 Zend 2 中,您可以将要在视图中使用的任何变量传递给 ViewModel
或 return 数组:
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