【问题标题】:How to call controller function in view in Zend Framework 2?如何在 Zend Framework 2 的视图中调用控制器函数?
【发布时间】: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


【解决方案1】:

在被认为是不好的做法中调用控制器操作。但是你可以通过使用视图助手来实现。所以你需要的是:

  • 创建您的自定义 View Helper,
  • module.config.php 的可调用对象中注册视图助手,
  • 然后您可以在视图中调用任何控制器操作

这是一个你可以使用的助手:

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

【讨论】:

  • zend 3 问题:我们如何获取 cms 页面标题及其链接以在整个网站的页眉/页脚部分显示它们。页面标题和链接存储在数据库中。请分享任何建议。谢谢。
【解决方案2】:

根据您需要实现 viewhelpers 的 cmets 我在这里找到了一个非常简单的解决方案。这可能对你也有用。

ZF2 - How can i call a function of a custom class php from a view?

这里

https://samsonasik.wordpress.com/2012/07/20/zend-framework-2-create-your-custom-view-helper/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    相关资源
    最近更新 更多