用于URI页面的Zend_Navigation和Zend_Acl

时间:2012-05-08 15:40:57

标签: zend-framework zend-navigation zend-acl

我正在尝试以独立方式(即仅这些ZF库)将Zend_NavigationZend_Acl组合为URI页面。不幸的是,即使我的意思是某个角色没有看到某些页面,我也会在菜单中看到它们(虽然我打电话给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
  )
);

我怎样才能完成上述工作?谢谢!

1 个答案:

答案 0 :(得分:1)

将资源与此类网页相关联

$pages = array(
    array('id' => '1', ... , 'resource' => '1'),
    array('id' => '2', ... , 'resource' => '2'),
    array('id' => '3', ... , 'resource' => '3')
);

其余的都很好。