【发布时间】:2022-01-04 07:33:22
【问题描述】:
我需要在视图中调用控制器函数并传递参数。我尝试关注此How to call controller function in view in Zend Framework?,但仍然无法正常工作。
我的数据库中有这样的记录:
---------------
| name | age |
---------------
| Josh | 22 |
| Bush | 43 |
| Rush | 23 |
---------------
这是我的 index.phtml
foreach ($result as $rstd){
echo "<td>".$this->escapeHtml($rstd['name'])."</td>";
echo "<td>".$this->escapeHtml($rstd['age'])."</td>";
//here i want to access my controller function with sending parameter by name and also display something which has i set in that function.
echo "<td>** result from that function **</td>";
}
这是我的控制器:
public function indexAction(){
$result = $sd->getAllRecord($this->getMysqlAdapter());
return new ViewModel(array('result'=>$result));
}
public function getRecordByName($name){
if($name=='Bush'){
$result = "You'r Old";
}else{
$result = "You'r Young";
}
return $result;
}
我想像这样显示它:
-----------------------------
| name | age | status |
-----------------------------
| Josh | 22 | You'r Young |
| Bush | 43 | You'r Old |
| Rush | 32 | You'r Young |
-----------------------------
你能帮帮我吗?
【问题讨论】:
-
无论你在
getRecordByName中做了什么,如果你想做同样的事情,你可以直接在视图中做。但您似乎希望编写一些可以在视图中使用的常用函数? -
为什么您认为 Zend 1 解决方案适用于 Zend Framework 2?还建议清洗半熟的解决方案以制作视图助手,然后传递整个控制器对象)
-
@Richie 是的,getRecordByName 只是一个简单的例子。实际上我会在那个函数中做一些复杂的事情。而且我不能在视图中做同样的事情,因为它很难实现,而且它只能在控制器中工作,而不是在视图中。那么你有解决方案吗..?
-
zend 3 问题:我们如何获取 cms 页面标题及其链接以在整个网站的页眉/页脚部分显示它们。页面标题和链接存储在数据库中。请分享任何建议。谢谢。
标签: php zend-framework2