Add Zend_Navigation to the View with old legacy bootstrap

这一生的挚爱 提交于 2019-12-13 02:37:06

问题


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


回答1:


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;
}


来源:https://stackoverflow.com/questions/2567360/add-zend-navigation-to-the-view-with-old-legacy-bootstrap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!