Zend Framework 2: Zend_Navigation

依然范特西╮ 提交于 2019-12-04 22:00:35
Fge

Assuming you're using beta5 - you're just configuring your viewhelper wrong. See this post on how to do it correctly.

On how to use navigation: I'd create a service in the servicemanager called navigation and put my navigation into a new config-key:

return array(
    'navigation' => array(
        // your navigation config goes here
    ),
    'servicemanager' => array(
        'factories' => array(
            'navigation' => function($sm) {
                $config = $sm->get('Configuration');
                $navigation = new \Zend\Navigation\Navigation($config->get('navigation'));
                return $navigation;
            }
        ),
    ),
    'view_helpers' => array(
        'factories' => array(
            'navigation' => function($sm) {
                return new \My\View\Helper\Navigation($sm->get('navigation'));
            }
        ),
    ),
);

(Not sure about the navigation class names. Haven't had a look on them.)

This does three things:

  • Provide a service called navigation pointing to the actual instance if zend's navigation
  • Provide your view helper and hand it a reference to the instance of your navigation
  • Create a new top-level key navigation containing the navigation configuration. Other modules can add custom navigation here without changing anything in your setup.

For example in your controllers you code fetch the navigation instance by calling

$this->getServiceLocator()->get('navigation');

while in your view helper has access to the navigation by it's constructor:

Navigation extends // ...
{
    public function __construct($navigation)
    {
        // do anything you want
    }
}

Other modules can add entries to your navigation by writing into the same key:

return array(
    'navigation' => array(
        // further navigation entries
    ),
);

Where you put the initial logic (e.g. setting up the services/view helpers) is up to you. I prefer writing a own module for this which can be disabled with a single line of code (resulting in the navigation not being present anymore). But the default module is probably a good place as well. Ultimately you could create your own factory-classes for the navigation and view helper instead of mixing the configuration with your code.

Disclaimer: Code's just a draft - not tested.

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