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(
	    'label' => 'Home',
            'route' => 'home',
            'resource'=> 'home',
	    'label' => 'Login',
            'route' => 'login',
            'resource'=> 'login',
	    '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

9 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 ?

  6. RK wrote:

    Thank you very much!

  7. Webdevilopers (@webdevilopers) wrote:

    @Micheal Wilkinson:
    If you need to check permissions on page in your partial you can use the accept() method:

    foreach ($this->container as $page){
    if($this->navigation()->accept($page)) {
    echo $this->navigation()->menu()->htmlify($page);

    Source: http://blog.webdevilopers.net/checking-acl-in-zend-navigation-when-using-custom-view-script/

  8. kourosh wrote:

    Hello, nice article, thank you.

    but i’m wondering how to pass user role to checkAcl function in module class ?

    In previous article we do like this:

    $route = $e->getRouteMatch()->getMatchedRouteName();

    $userRole = ‘member’;

    if (!$e->getViewModel()->acl->isAllowed($userRole, $route))

    But how do it in this way ?

  9. Ivan Gospodinow wrote:

    the user role, most of the times is stored in the user object, right?
    Then you can have a new instance of Zend\Session and get your current user.
    If user does not exists in the session, then the role can be guest.

Leave Your Comment

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