2012-07-25 111 views
2

所以我通常都采用“URL”动作助手从定制动作助手没有问题,访问模块时,使用下列内容:从默认模块自定义动作助手访问Zend的URL动作助手

$urlHelper = Zend_Controller_Action_HelperBroker::getExistingHelper('url'); 

但是,如果访问默认模块(根url,/),则会发生以下错误:

致命错误:带有消息'Action helper'的未捕获异常'Zend_Controller_Action_Exception'尚未向helper broker注册/home/erahhal/Code/ZendFramework-1.11.12/library/Zend/Controller/Plugin/Broker.php on line 336

这个问题的根源是什么?

+0

我不知道你已经给了足够的信息来解决问题。你如何配置你的ZF应用程序?配置是什么样的?这只是一个URL,还是问题发生在你的默认模块的其他地方?它也发生在其他模块吗?您收到的异常是否也说明了HelperBroker正在查找的文件夹? – curtisdf 2012-07-25 19:51:16

回答

2

通常,如果我想使用的网址助手控制器或动作助手的背景之外,我刚刚创建帮助自己的新实例。

您应该可以使用下面的代码来获得一个网址助手,并使用它:

$urlHelper = new Zend_Controller_Action_Helper_Url(); 
$url  = $urlHelper->url(array('controller' => 'foo', 
            'action'  => 'bar', 
            'module'  => 'mod')); 

我不知道为什么你遇到这个错误,但如果助手尚未注册与前端控制器(?也许你正在调用这个太早在你的应用调度),请尝试使用getStaticHelper()代替getExistingHelper()

$urlHelper = Zend_Controller_Action_HelperBroker::getStaticHelper('url'); 

如果URL助手尚未与插件加载注册,它将注册并加载它你呢。

The Helper Broker Documentation

There are also two static methods for retrieving helpers from the helper broker: getExistingHelper() and getStaticHelper(). getExistingHelper() will retrieve a helper only if it has previously been invoked by or explicitly registered with the helper broker; it will throw an exception if not. getStaticHelper() does the same as getExistingHelper(), but will attempt to instantiate the helper if has not yet been registered with the helper stack. getStaticHelper() is a good choice for retrieving helpers which you wish to configure.

+0

感谢您的帮助。我试过getHelper,但仍然有一个致命的错误。最终工作的是getStaticHelper,如果它不存在,它会实例化助手。 – colordrops 2012-07-27 18:46:22

+0

我刚刚根据你的发现更新了答案。它看起来像getHelper会为你注册但不是。我添加了一个来自文档的参考,它解释了getStaticHelper如何在您找到时为您实例化它。 – drew010 2012-07-27 18:56:59