我正在尝试以独立的方式组合Zend_Navigation
和Zend_Acl
用于 URI 页面(即只有这些 ZF 库)。不幸的是,即使我的意思是让某个角色看不到某些页面,我也会在菜单中看到它们(尽管如果我调用isAllowed
,一切都会按预期工作)。
遵循显示我的问题的代码片段:
$pages = array (
array( 'id' => '1', 'label' => 'page 1', 'uri' => '1.html', 'visible' => 1 ),
array( 'id' => '2', 'label' => 'page 2', 'uri' => '2.html', 'visible' => 1 ),
array( 'id' => '3', 'label' => 'page 3', 'uri' => '3.html', 'visible' => 1 )
);
$nav = new Zend_Navigation($pages);
$acl = new Zend_Acl();
$acl->addRole(new Zend_Acl_Role(1));
$acl->addRole(new Zend_Acl_Role(2));
$acl->addResource(new Zend_Acl_Resource(1));
$acl->addResource(new Zend_Acl_Resource(2));
$acl->addResource(new Zend_Acl_Resource(3));
$acl->allow(1, 1);
$acl->allow(1, 2);
$acl->allow(1, 3);
$acl->allow(2, 1);
// Role 1 sees 1,2,3 while Role 2 only 1
$view = new Zend_View();
$menu = new Zend_View_Helper_Navigation_Menu();
$menu->setView($view)
->setContainer($nav)
->setTranslator($translate)
->setAcl($acl)->setRole('2'); //tried both this or ->setAcl($acl) ->setRole('2');
echo "2 can't see 2 and 3, right? " . ((!$acl->isAllowed('2', '2'))?"right":"not right") . "\n";
echo $menu->menu()->renderMenu(
null,
array(
'minDepth' => 0,
'maxDepth' => 1,
'onlyActiveBranch' => false,
'renderParents' => true
)
);
我怎样才能使上述工作?谢谢!!