ZF2 - 带导航的多导航

时间:2012-11-20 16:16:59

标签: zend-framework2

是否可以有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个不同页面的不同页面,但这种方法不会运行。

每个人都有想法吗?

由于

Birzat

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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(); ?>