5

是否可以有 2 个不同的导航?

例如 :

//in module.config.php
'service_manager'=>array(
        'factories'=>array(
            'navigation1'=>'Zend\Navigation\Service\DefaultNavigationFactory',
            'navigation2'=>'Zend\Navigation\Service\DefaultNavigationFactory',
        ),
    ),
    'navigation'=>array(
        'navigation1'=>array(
            'home'=>array('type' => 'mvc','route' => 'home','active'=>false,'label' => 'Home','title' => 'Home',
                'pages'=>array(
                    'contact'=>array('type' => 'mvc','route'=>'contact','active'=>false,'label'=>'Contact','title' =>'Contact'),
                )
            ),
        ),
        'navigation2'=>array(
            'home'=>array('type'=>'mvc','route'=>'home','active'=>false,'label'=>'Home','title'=>'Home',
            'contact'=>array('type'=>'mvc','route'=>'faq','active'=>false,'label'=>'Faq','title'=>'Faq'),
            ),
        ),

//Dans laout
<?php echo $this->navigation()->menu('navigation1')->setMinDepth(0);?>
<hr />
<?php echo $this->navigation()->menu('navigation2')->setMinDepth(0);?>

我想要 2 个不同页面的不同菜单,但此方法无法运行。

请问大家有什么想法吗?

谢谢

比尔扎特

4

2 回答 2

6

您需要为每个导航组提供一个自定义工厂类。例如,看看 ZfcAdmin 如何做到这一点:

  1. 创建自定义工厂类

    <?php
    namespace ZfcAdmin\Navigation\Service;
    
    use Zend\Navigation\Service\DefaultNavigationFactory;
    
    class AdminNavigationFactory extends DefaultNavigationFactory
    {
        protected function getName()
        {
            return 'admin';
        }
    }
    

    来源:https ://github.com/ZF-Commons/ZfcAdmin/blob/master/src/ZfcAdmin/Navigation/Service/AdminNavigationFactory.php

  2. 注册 AdminNavigationFactory

    // in Module.php
    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'admin_navigation' => 'ZfcAdmin\Navigation\Service\AdminNavigationFactory',
            ),
        );
     }
    

    来源:https ://github.com/ZF-Commons/ZfcAdmin/blob/master/Module.php#L90

  3. getName在您在工厂方法中指定的键下定义模块配置中的导航树。例如,这是 ZfcUserAdmin 将自身添加到 ZfcAdmin 菜单的方式:

    'navigation' => array(
        'admin' => array(
            'zfcuseradmin' => array(
                'label' => 'Users',
                'route' => 'zfcadmin/zfcuseradmin/list',
                'pages' => array(
                    'create' => array(
                        'label' => 'New User',
                        'route' => 'admin/create',
                    ),                        
                ),
            ),
        ),
    ),
    

    来源:https ://github.com/Danielss89/ZfcUserAdmin/blob/master/config/module.config.php

于 2012-11-21T13:10:52.600 回答
4

/vendor/MyNamespace/library/MyNamespace/Navigation/Service/SecondaryNavigationFactory.php

namespace MyNamespace\Navigation\Service;

use Zend\Navigation\Service\DefaultNavigationFactory;

class SecondaryNavigationFactory extends DefaultNavigationFactory {

    protected function getName() {
        return 'secondary';
    }

}

/config/autoload/global.php

return array(
    'service_manager' => array(
        'factories' => array(
            'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
            'secondary' => 'MyNamespace\Navigation\Service\SecondaryNavigationFactory',
        ),
    ),
    'navigation' => array(
        'default' => array(
            array(
                'label' => 'Item-1.1',
                'route' => 'foo',
            ),
            array(
                'label' => 'Item-1.2',
                'route' => 'bar',
            ),
        ),
        'secondary' => array(
            array(
                'label' => 'Item-2',
                'route' => 'baz',
            ),
        ),
    ),
);

/module/Application/view/layout/layout.phtml

<?php echo $this->navigation('navigation')->menu(); ?>
<?php echo $this->navigation('secondary')->menu(); ?>
于 2013-04-11T20:20:45.213 回答