开发者

Zend: How can I globally set the current navigation item?

开发者 https://www.devze.com 2022-12-17 12:00 出处:网络
config: resources.frontController.plugins.nav = \"WD_Controller_Plugin_Nav\" bootstrap: protected function _initAutoloader() {

config:

resources.frontController.plugins.nav = "WD_Controller_Plugin_Nav"

bootstrap:

protected function _initAutoloader() {
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('WD_');
return $loader;
}

protected function _initNav() {
  $this->bootstrap('layout');
  $layout = $this->getResource('layout');
  $view = $layout->getView();
  $config = new Zend_Config_Xml( APPLICATION_PATH . '/configs/navigation.xml', 'nav');
  $navigation = Zend_Navigation( $config );

  $fc = Zend_Controller_Front::getInstance();
  $fc->registerPlugin( new WD_Controller_Plugin_Nav() );
  $view->navigation( $navigation );
}

library/WD/Controller/Plugin/Nav.php:

class WD_Controller_Plugin_Nav extends Zend_Controller_Plugin_Abstract {

    public function postDispatch() {
        $uri = $this->_request->getPathInfo();
            $view = Zend_Layout::getMvcInstance()->getView();
        $activeNav = $view->navigation()->findByUri($uri)->setActive(true);
    }
}

web output:

Uncaught exception 'Zend_Loader_PluginLoader_Exception' with message 'Plugin by name 'FindByUri' was not found in the registry

I pretty much know what I'm doing wrong, in that I'm referencing the Zend View Helper Navigation and n开发者_StackOverflowot the Navigation directly ( findByUri method exists in Zend_Navigation directly ), but I'm not sure how to properly reference it.


Controller plugins do not have a reference to the view object by default, like Controllers do. There are plenty of different ways to grab your view instance.

One, via the ViewRenderer action helper, like so:

$view = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view;

Two, from your layout:

$view = Zend_Layout::getMvcInstance()->getView();

Or, as always, from your application bootstrap. But that will depend on how you set it up.

Once you do that, there is a getContainer() method available to you on all Navigation view helpers. So,

$container = $view->navigation()->getContainer();

Should work for you,

0

精彩评论

暂无评论...
验证码 换一张
取 消