Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Zend Framework 2中调用视图中的控制器函数?_Php_Zend Framework2 - Fatal编程技术网

Php 如何在Zend Framework 2中调用视图中的控制器函数?

Php 如何在Zend Framework 2中调用视图中的控制器函数?,php,zend-framework2,Php,Zend Framework2,我需要在视图中调用一个控制器函数并传递参数。我试着跟踪这个,但仍然不起作用 我的数据库中有如下记录: --------------- | name | age | --------------- | Josh | 22 | | Bush | 43 | | Rush | 23 | --------------- ----------------------------- | name | age | status | --------------------------

我需要在视图中调用一个控制器函数并传递参数。我试着跟踪这个,但仍然不起作用

我的数据库中有如下记录:

---------------
| name  | age |
---------------
| Josh  | 22  |
| Bush  | 43  |
| Rush  | 23  |
---------------
-----------------------------
| name  | age | status      |
-----------------------------
| Josh  | 22  | You'r Young |
| Bush  | 43  | You'r Old   |
| Rush  | 32  | You'r Young |
-----------------------------
这是我的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>";
}
我想这样显示它:

---------------
| name  | age |
---------------
| Josh  | 22  |
| Bush  | 43  |
| Rush  | 23  |
---------------
-----------------------------
| name  | age | status      |
-----------------------------
| Josh  | 22  | You'r Young |
| Bush  | 43  | You'r Old   |
| Rush  | 32  | You'r Young |
-----------------------------

您能帮助我吗?

根据您实施viewhelpers所需的注释 我在这里找到了一个非常简单的解决办法。这可能对你也有用

这里呢


在考虑过的不良做法中调用视图中的控制器操作。但您可以通过使用视图帮助器来实现这一点。因此,您需要的是:

  • 创建自定义视图辅助对象
  • module.config.php
    中的invokables中注册视图帮助程序
  • 然后可以在视图中调用任何控制器操作
以下是您可以使用的帮助器:

class Action extends \Zend\View\Helper\AbstractHelper implements   ServiceLocatorAwareInterface
{

    protected $serviceLocator;

    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
        return $this;
    }

    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }
    public function __invoke($controllerName, $actionName, $params = array())
    {
        $controllerLoader = $this->serviceLocator->getServiceLocator()->get('ControllerLoader');
        $controllerLoader->setInvokableClass($controllerName, $controllerName);
        $controller = $controllerLoader->get($controllerName);
        return $controller->$actionName($params);
    }
}
module.config.php:

'view_helpers' => array(
'invokables' => array(
    'action' => 'module_name\View\Helper\Action',
),  
),
在您的视图文件中:

$this->action('Your\Controller', 'getRecordByNameAction');

希望这能有所帮助。

无论您在
getRecordByName
中做了什么,如果您想做同样的事情,可以直接在视图中完成。但您似乎希望编写一些可以在视图中使用的常用函数?为什么您认为Zend 1解决方案适用于Zend Framework 2?另外,建议使用“清洗半成品”解决方案,以使视图助手传递整个控制器对象)@Richie yeah,getRecordByName只是一个简单的示例。实际上,我会在这个函数中做一些复杂的事情。我不能在视图中做同样的事情,因为它很难实现,而且它只能在控制器中工作,而不能在视图中工作。zend 3问题:我们如何获取cms页面标题及其链接,将其显示在整个网站的页眉/页脚部分。页面标题和链接存储在数据库中。请分享任何建议。zend 3问题:我们如何获取cms页面标题及其链接,以便在整个网站的页眉/页脚部分显示它们。页面标题和链接存储在数据库中。请分享任何建议。谢谢