引导布局资源后将变量传递给布局
我必须在 Boostrap 中创建方法来引导布局资源并注册一些视图助手。
protected function _initViewHelpers() {
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->registerHelper(new Application_View_Helper_LoadMenu, 'loadMenu');
$view->registerHelper(new Application_View_Helper_InfoLink, 'infoLink');
$view->registerHelper(new Application_View_Helper_InfoData, 'infoData');
}
现在,我将一些变量传递给布局(一如既往地传递给 Zend_View 实例),但布局无法识别它具有它们。
当我将注册助手的代码移动到控制器中的 init() 方法时,一切正常。是ZF错误还是我做错了什么?
I had to create method in Boostrap which bootstraps Layout resource and registers some view helpers.
protected function _initViewHelpers() {
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->registerHelper(new Application_View_Helper_LoadMenu, 'loadMenu');
$view->registerHelper(new Application_View_Helper_InfoLink, 'infoLink');
$view->registerHelper(new Application_View_Helper_InfoData, 'infoData');
}
Now, I am passing some variables to layout (to Zend_View instance, as always), but layout doesn't recognize that it has them.
When I move code which registers helpers, to init() method in controller, everything is ok. Is it ZF error or I did sth wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的控制器(或您有视图的任何地方)中,
在您的布局中,
如果我遗漏了您问题的某些部分,请告诉我。
编辑:如果上述方法失败,另一种正确的方法是使用占位符助手(http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial .placeholder)
编辑2:确保您也在引导视图。
In Your Controller (or wherever you have view)
In Your Layout
if i'm missing some part of your question let me know.
Edit: failing the above, the other correct way to do this would be to use the placeholder helper (http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.placeholder)
Edit 2: Make sure you are bootstrapping view as well.
您的视图助手是否直接实现了
setView()
方法,或者可能作为Zend_View_Helper_Abstract
的子类实现?如果您查看
Zend_View_Abstract::registerHelper($helper, $name)
方法的代码,您将看到它检查是否存在setView()
方法帮手。如果找到这样的方法,则会调用$helper->setView($this)
,其中$this
是$view
。这就是连接发生的地方。在没有这个调用的情况下,似乎虽然视图会意识到帮助器(毕竟,您刚刚注册了它),但帮助器将不知道视图。如果助手尝试访问视图,它最终会创建一个新的视图对象,该对象不是您在 Bootstrap 中配置的对象。
tl;dr:可能不需要显式注册帮助程序。使用默认的资源自动加载器和您似乎正在使用的类/方法命名约定,您可能可以允许内置插件加载器处理所有实例化。只需在布局或视图脚本中调用
$this->myHelperMethod()
,一切都会很酷。Do your view-helpers implement the
setView()
method, either directly or perhaps as subclasses ofZend_View_Helper_Abstract
?If you look at the code for
Zend_View_Abstract::registerHelper($helper, $name)
method, you will see that it checks for the presence of asetView()
method on the helper. If it finds such a method, then it calls$helper->setView($this)
, where$this
is the$view
.This is where the connection takes place. In the absence of this call, it seems that although the view will be aware of the helper (after all, you did just register it), the helper will be unaware of the view. If the helper tries to access the view, it ends up creating a new view object, which is not the one you configured way back in Bootstrap.
tl;dr: There is probably no need to explicitly register the helpers. With the default resource-autoloader in place and the class/method naming convention you seem to be using, you can probably allow the built-in plugin-loader to handle all the instantiation. Simply call
$this->myHelperMethod()
in your layouts or view-scripts and all should be cool.