zend-framework2zend-navigationzend-acl

Second navigation not injecting ACL


I have an application with an admin area which requires a separate navigation menu from the main site, I also need to inject the ACL to prevent the admin user from seeing certain menu items.

I have created an admin navigation factory.

namespace Freedom\Zend\Navigation\Service;

use Zend\Navigation\Service\DefaultNavigationFactory;

class AdminNavigationFactory extends DefaultNavigationFactory
{
    protected function getName()
    {
            return 'admin_navigation';
    }
}

and added it to the service manager

'service_manager' => array(
        'factories' => array(
            'admin_navigation' => 'Freedom\Zend\Navigation\Service\AdminNavigationFactory'
        ),
    ),

This all works fine and I can see the menu, the problem is when I try to inject the ACL. In my admin module.php I have

public function getViewHelperConfig()
    {
        return array(
            'factories' => array(
                'admin_navigation' => function(HelperPluginManager $pluginManager) {
                    $serviceManager = $pluginManager->getServiceLocator();

                    $acl = new Acl();

                    $auth = $serviceManager->get('Zend\Authentication\AuthenticationService');

                    $role = $acl::DEFAULT_ROLE;

                    if ($auth->hasIdentity()) {
                        $user = $auth->getIdentity();
                        $role = $user->getUserRole()->getRole();
                    }

                    $navigation = $pluginManager->get('Zend\View\Helper\Navigation');

                    $navigation->setAcl($acl)->setRole($role);
                    return $navigation;
                }
            )
        );
    }

However the admin_navigation function above is not being called. In my default navigation in my application module I have almost identical code and the function is being called ok.

Does anyone know why the admin_navigation function is not being called?

Many thanks in advance.


Solution

  • I have figured it out with the help of Stoyan Cheresharov, https://www.youtube.com/watch?v=6U_lERLu4Ys. admin_navigation should just be navigation.