ZF2注册自定义帮助程序进行导航

问题描述:

我正在将 Zend Navigation ACL 一起使用.我的用户可以具有多个角色,彼此之间没有关系,但是 Zend Navigation 仅接受一个角色,并检查具有该角色的 ACL ,这对我不利.

I am using Zend Navigation with ACL. My users can have multiple roles, which have no relation to each other, but Zend Navigation only accepts one role and checks the ACL with that role which is not good for me.

如何为导航注册新的助手,以便可以覆盖acceptAcl方法.我试图创建并注册一个简单的 view helper ,但这没用

How can I register a new Helper for the Navigation such that I can override the acceptAcl method. I tried to create and register a simple view helper but that didn't work

class Menu extends \Zend\View\Helper\Navigation\Menu implements \Zend\ServiceManager\ServiceLocatorAwareInterface
{

    protected function acceptAcl(AbstractPage $page)
    {
        if (!$acl = $this->getAcl()) {
            // no acl registered means don't use acl
            return true;
        }

        $userIdentity = $this->getServiceLocator()->get('user_identity');
        $resource = $page->getResource();
        $privilege = $page->getPrivilege();

        $allowed = true;
        if ($userIdentity->id !== "1") {

            if ($acl->hasResource($resource)) {
                $allowed = false;
                foreach ($userIdentity->rolls as $roll) {
                    if ($acl->isAllowed($roll['id'], $resource)) {
                        $allowed = true;
                        continue;
                    }
                }
            }
        }

        return $allowed;
    }

    public function renderMenu($container = null, array $options = array())
    {
        return 'this is my menu';
    }

}

'view_helpers' => array(
    'invokables' => array(
        'myMenu' => 'Application\View\Helper\Menu',
    ),
),

如何注册该帮助程序,以便导航系统了解它?

How can I register this helper so that the Navigation knows about it?

Navigation视图帮助器已在ViewHelperPluginManager上注册.所有导航助手(MenuBreadcrumbs等)均由单独的pluginmanager管理.据我所知,您还不能覆盖配置中的导航助手.

The Navigation view helper is registered on the ViewHelperPluginManager. All the navigation helpers (Menu, Breadcrumbs etc) are managed by a seperate pluginmanager. As far as I know you cannot overwrite the navigation helpers in your configuration yet.

尝试将以下内容添加到您的Module.php:

Try to add the following to your Module.php:

class Module
{
    public function onBootstrap($e)
    {
        $application = $e->getApplication();
        /** @var $serviceManager \Zend\ServiceManager\ServiceManager */
        $serviceManager = $application->getServiceManager();

        $pm = $serviceManager->get('ViewHelperManager')->get('Navigation')->getPluginManager();
        $pm->setInvokableClass('myMenu', '\Application\View\Helper\Menu');
    }
}