Zend Framework 2: Zend_Navigation

2019-04-17 01:53发布

I am just wondering what would be the best way, to use Zend_Navigation in Zend Framework2. At the moment i am trying to a add a Config for a Header and a Footer Menu. Both Menus should be populated automatically. Though, i have run into a lot of Problems:

a) The best place for the Default Menu should be hopefully the Application-Module

b) I added the following to the root of module.config.php:

'view_helpers' => array(
    'helper_map' => array(
            'navigation' => 'Application\View\Helper\Navigation'
    ),
),

My Navigation.php is located in ./module/Application/src/Application/View/Helper/Navigation.php

and contains:

class Navigation extends AbstractContainer {
    public function __construct($menu = null) {
        var_dump($menu);
    }
}

I know that the Class may be wrong. But at the Moment i did not get ZF2 to even load the Class. What is wrong ?

1条回答
放我归山
2楼-- · 2019-04-17 02:48

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.

查看更多
登录 后发表回答