Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZF2 - Multi navigation with navigation

Is it possible to have 2 differents navigation ?

For example :

//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);?>

I would like 2 differents menu with differents pages but this method doesn't run.

Every one has an idea please ?

Thanks

Birzat

like image 909
SayDevNet Avatar asked Nov 20 '12 16:11

SayDevNet


2 Answers

You need to provide a custom factory class for each navigation group. For example, see how ZfcAdmin does this:

  1. Create a custom factory class

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

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

  2. Register AdminNavigationFactory

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

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

  3. Define Navigation trees in your module's configuration under the key you specified in the getName method of your factory. As an example, this is how ZfcUserAdmin adds itself to the ZfcAdmin menu:

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

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

like image 126
Adam Lundrigan Avatar answered Sep 17 '22 05:09

Adam Lundrigan


/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(); ?>
like image 43
automatix Avatar answered Sep 20 '22 05:09

automatix