Pankrates
Pankrates

Reputation: 3094

ZF2 injecting ACL into navigation

I have a working ACL module and Navigation setup in a Zend Framework 2 project. The navigation object is generated and rendered properly, but it contains an element admin that I only want to be rendered when the logged in user has the role admin. The acl is properly blocking access (i.e. redirects to 403 page) when a non-admin tries to access any pages there, however the navigation still renders the nav-item. The navigation setup is very simple (as it should be based on the read tutorials)

module.config.php

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

view.phtml

<div id="navigation">
    <?php echo $this->navigation('navigation')
                    ->menu()
                    ->setMinDepth(0)
                    ->setMaxDepth(0)
                    ->setAcl($this->acl)
                    ->setRole('guest'); ?>
</div>

After a lot of debugging, I pinpointed where something is going wrong but I have no clue why or how to fix it. I tried var_dumping $this->acl in the view and it is set, as expected. However:

var_dump($this->navigation('navigation')->getAcl());

after the line with setAcl(), returns NULL. So somehow it seems that setAcl($this->acl) is not actually injecting the acl.

Upvotes: 2

Views: 1839

Answers (2)

Twi Light
Twi Light

Reputation: 11

Somewhere in module bootstrap

\Zend\View\Helper\Navigation::setDefaultAcl($acl);
\Zend\View\Helper\Navigation::setDefaultRole($role);

Upvotes: 0

Jaimie
Jaimie

Reputation: 361

I had the same problem and solved it by adding setAcl and setRole before menu:

echo $this->navigation('navigation')->setAcl($this->acl)->setRole($this->role)->menu()->setPartial('partial/navbar.phtml');

Upvotes: 1

Related Questions