整个周末我一直在和Zend_Navigation斗争,现在我还有另外一个问题,我认为这是我的很多问题的原因。
我正在尝试将Zend_Navigation添加到遗留的1.7.6 Zend Framework应用程序中,我已将Zend Library更新为1.9.0并更新了引导程序以允许此库更新。
问题是我不知道如何,并且示例显示了如何将Navigation对象添加到视图的新bootstrap方法,我试过这个:
//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);
这似乎运行得很好,但是当我在布局中使用breadcrumb视图助手时,它出错了:严格标准:在C:\ www \ moobia \ development \中从空值创建默认对象第27行的网站\ application \ modules \ employer \ controllers \ IndexController.php
这是由我的控制器的init()函数中的以下代码引起的。
$uri = $this->_request->getPathInfo();
$activeNav = $this->view->navigation()->findByUri($uri); <- this is null when called
$activeNav->active = true;
我相信这是因为Zend_Navigation对象不在视图中。
我会考虑将引导程序迁移到当前方法,但目前我没有时间进行发布。
谢谢,
捐赠
答案 0 :(得分:2)
首先,您需要弄清楚您是否怀疑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;
}