【问题标题】:How to call Action Helper as broker method in View Helper?如何在 View Helper 中调用 Action Helper 作为代理方法?
【发布时间】:2011-10-01 21:43:03
【问题描述】:

我有 Action Helper,它是数据库抽象层。

我想在 View Helper 中访问它以读取和呈现模型中的一些数据。

在Controller中我调用Action Helper作为代理方法,但是在View Helper中如何实现呢?

控制器中的某处:

$this->_helper->Service("Custom\\PageService");

Service.php:

...
public function direct($serviceClass)
{
    return new $serviceClass($this->em);
}

【问题讨论】:

    标签: zend-framework view-helpers


    【解决方案1】:

    更好的方法是在其中创建一个视图助手

      Zend_Controller_Action_HelperBroker::getStaticHelper('service')->direct("Custom\\PageService");
    

    另一种方法是在控制器的 init 方法中做

    $this->view->helper = $this->_helper;
    

    所以在视图(phtml)中你可以这样做

    $this->helper->Service("Custom\\PageService");
    

    【讨论】:

    • 问题仍然存在,这将是更好的解决方案。我认为第一个是因为你不依赖Controller。你怎么看?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    相关资源
    最近更新 更多