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.
Anton Shumanski wrote:
Great article, but how I can add params to the links in menu?
Link | May 8th, 2013 at 12:47
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
Link | June 24th, 2013 at 15:33
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.
Link | June 24th, 2013 at 17:36
RK wrote:
Other excellent tutorial, thank you so much. But how can I create another menu – for back end for example. Thanks in advance!
Link | October 8th, 2013 at 10:54
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 ?
Link | October 8th, 2013 at 23:01
RK wrote:
Thank you very much!
Link | October 9th, 2013 at 14:59
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/
Link | June 5th, 2014 at 09:15
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 ?
Link | July 5th, 2014 at 13:37
Ivan Gospodinow wrote:
Hello,
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.
Link | July 5th, 2014 at 14:36