Zend Framework 2 Navigation with ACL in 3 steps

May 2, 2013

This post is a  follow up from : zend framework 2 acl in 5 minutes

Step 1

Code in config/autoload/global.php

'navigation' => array(
    'default'=> array(
      	array(
	    'label' => 'Home',
            'route' => 'home',
            'resource'=> 'home',
        ),
      	array(
	    'label' => 'Login',
            'route' => 'login',
            'resource'=> 'login',
        ),
      	array(
	    'label' => 'Register',
            'route' => 'register',
            'resource'=> 'register',
        ), 
    )
)

Step 2

Code in layout.phtml

echo $this->navigation('Navigation')->setAcl($this->acl)->setRole('guest');

Step 3

Add navigation factory in the config

Code in module.config.php

'service_manager' => array(
    'factories' => array(
       'Navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
    ),
 ),

Simple as that.

Suggestions or problems ? Write a comment.

tags: , ,
posted in Zend Framework 2 by Ivan Gospodinow

Follow comments via the RSS Feed | Leave a comment | Trackback URL

5 Comments to "Zend Framework 2 Navigation with ACL in 3 steps"

  1. Anton Shumanski wrote:

    Great article, but how I can add params to the links in menu?

  2. Micheal Wilkinson wrote:

    This works perfectly for me however I want to use a partial view script and I was wondering if you knew how to access the ACL information within the partial script

  3. Ivan Gospodinow wrote:

    Hello Micheal ,

    as the acl object in injected in the layout , so you can access it in any view helper like this :
    $acl = $this->getView()->layout()->acl;

    I suggest that do not use acl logic in the partial loop.
    If you share your problem , may be I can help with a better solution.

  4. RK wrote:

    Other excellent tutorial, thank you so much. But how can I create another menu – for back end for example. Thanks in advance!

  5. Ivan Gospodinow wrote:

    Hello RK,
    you can add a factory for the navigation like this:

    Go to config -> service_manager -> factories
    Add ‘AdminNavigation’ => ‘Application\Factory\AdminNavigation’

    Then in that file you have :
    class AdminNavigation extends Zend\Navigation\Service\AbstractNavigationFactory
    {
    function getName()
    {
    return ‘admin_menu’;
    }
    }

    Then you go to your navigation array and add key : ‘admin_menu’ and its pages array.

    Got it ?

Leave Your Comment

 
 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org