引导布局资源后将变量传递给布局

发布于 2024-12-29 21:33:47 字数 587 浏览 4 评论 0原文

我必须在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哆兒滾 2025-01-05 21:33:47

在您的控制器(或您有视图的任何地方)中,

$view->layout()->some_var = "Some Value";

在您的布局中,

<?php echo $this->layout()->some_var; ?>

如果我遗漏了您问题的某些部分,请告诉我。

编辑:如果上述方法失败,另一种正确的方法是使用占位符助手(http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial .placeholder)

编辑2:确保您也在引导视图。

$this->bootstrap('view');
$view = $this->getResource('view');

In Your Controller (or wherever you have view)

$view->layout()->some_var = "Some Value";

In Your Layout

<?php echo $this->layout()->some_var; ?>

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.

$this->bootstrap('view');
$view = $this->getResource('view');
清引 2025-01-05 21:33:47

您的视图助手是否直接实现了 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 of Zend_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 a setView() 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文