使用旧的引导程序将 Zend_Navigation 添加到视图

发布于 2024-08-27 03:34:07 字数 1225 浏览 9 评论 0原文

整个周末我都在与 Zend_Navigation 作斗争,现在我遇到了另一个问题,我相信这是造成我很多问题的原因。

我正在尝试将 Zend_Navigation 添加到旧版 1.7.6 Zend Framework 应用程序中,我已将 Zend 库更新到 1.9.0 并更新了引导程序以允许此库更新。

问题是我不知道如何,并且示例显示了如何将导航对象添加到视图的新引导方法,我已经尝试过:

//initialise the application layouts with the MVC helpers
$layout = Zend_Layout::startMvc(array('layoutPath' => '../application/layouts'));

$view = $layout->getView();
$configNav = new Zend_Config_Xml('../application/config/navigation.xml', 'navigation');
$navigation = new Zend_Navigation($configNav);
$view->navigation($navigation);
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRenderer->setView($view); 

这似乎运行良好,但是当我去使用在我的布局中,面包屑视图助手出现错误:严格标准:从 C:\www\moobia\development\website\application\modules\employers\controllers\IndexController.php 中的空值创建默认对象第 27 行< /strong>

这是由我的控制器的 init() 函数中的以下代码引起的。

$uri = $this->_request->getPathInfo();
$activeNav = $this->view->navigation()->findByUri($uri); <- this is null when called
$activeNav->active = true;

我相信这是因为 Zend_Navigation 对象不在视图中。

我会考虑将引导程序迁移到当前方法,但目前我已经没有时间发布了。

谢谢,

格兰特

I've been struggling with Zend_Navigation all weekend, and now I have another problem, which I believe has been the cause of a lot of my issues.

I am trying to add Zend_Navigation to a legacy 1.7.6 Zend Framework application, i've updated the Zend Library to 1.9.0 and updated the bootstrap to allow this library update.

The problem is that I don't know how, and the examples show the new bootstrap method of how to add the Navigation object to the view, I've tried this:

//initialise the application layouts with the MVC helpers
$layout = Zend_Layout::startMvc(array('layoutPath' => '../application/layouts'));

$view = $layout->getView();
$configNav = new Zend_Config_Xml('../application/config/navigation.xml', 'navigation');
$navigation = new Zend_Navigation($configNav);
$view->navigation($navigation);
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRenderer->setView($view); 

This seems to run through fine, but when I go to use the breadcrumb view helper in my layout, it errors with: Strict Standards: Creating default object from empty value in C:\www\moobia\development\website\application\modules\employers\controllers\IndexController.php on line 27

This is caused by the following code in the init() function of my controller.

$uri = $this->_request->getPathInfo();
$activeNav = $this->view->navigation()->findByUri($uri); <- this is null when called
$activeNav->active = true;

I believe it's because the Zend_Navigation object is not in the view.

I would look at migrating the bootstrap to the current method, but at present I am running out of time for a release.

Thanks,

Grant

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

做个少女永远怀春 2024-09-03 03:34:07

首先,您需要确定您对 Zend_Navigation 不在视图中的怀疑是否正确。最简单的方法是将:添加

var_dump($this->view->navigation());exit;

到控制器 init() 中。这应该返回 Zend_Navigation 对象(如果存在)。

如果不存在,提供 Zend_Navigation 对象的另一种方法是使用注册表,这可能更容易。为此,您需要从引导程序中删除视图内容,然后执行以下操作:

$configNav = new Zend_Config_Xml('../application/config/navigation.xml', 'navigation');
$navigation = new Zend_Navigation($configNav);
Zend_Registry::set('Zend_Navigation', $navigation);

您的控制器 init() 内容将保持与视图对象在注册表中查找的内容相同(如果它还没有 Zend Navigation 对象)。

但是,我不确定您的控制器 init() 代码是否能够按照您想要的方式工作。我认为 findByUri() 不适用于 Mvc 页面(但我可能是错的),并且从您之前的问题来看,XML 文件中的大多数页面都是 Mvc 页面。 Mvc 类有一个“href”属性,它看起来是等价的。如果您的 XML 文件包含这两种页面类型,您可能需要检查这两种页面类型,所以我建议如下:

$uri = $this->_request->getPathInfo();
if (($activeNav = $this->view->navigation()->findByHref($uri)) !== null) {
    $activeNav->active = true;
} else if (($activeNav = $this->view->navigation()->findByUri($uri)) !== null) {
    $activeNav->active = true;
}

First you need to work out whether your suspicion that Zend_Navigation is not in the view is correct. Easiest way to do this would be to add:

var_dump($this->view->navigation());exit;

to your controller init(). This should return the Zend_Navigation object if it's there.

If it's not there, an alternative way of supplying the Zend_Navigation object is to use the registry, which might be easier. To do this you'd remove the view stuff from your bootstrap and just do this:

$configNav = new Zend_Config_Xml('../application/config/navigation.xml', 'navigation');
$navigation = new Zend_Navigation($configNav);
Zend_Registry::set('Zend_Navigation', $navigation);

your controller init() stuff would remain the same as the view object will look in the registry if it doesn't already have a Zend Navigation object.

However, I'm not sure that your controller init() code will quite work the way that you want. I don't think findByUri() will work on Mvc pages (but I could be wrong), and from your previous question it looked like most of the pages in your XML file are Mvc ones. The Mvc class has an 'href' property which appears to be the equivalent. If your XML file contains both page types, you might need to check both, so I'd suggest something like this:

$uri = $this->_request->getPathInfo();
if (($activeNav = $this->view->navigation()->findByHref($uri)) !== null) {
    $activeNav->active = true;
} else if (($activeNav = $this->view->navigation()->findByUri($uri)) !== null) {
    $activeNav->active = true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文