ZF2 register custom helper for navigation

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.

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?

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.

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');
    }
}

Navigation Helpers - Manual - Documentation, Navigation Helpers — Zend Framework 2 2.0.7 documentation __call() is used for proxying calls to the container registered in the helper, which means you can This example shows how to render a custom menu using a partial vew script. In this post I will show you how to create a custom view helper in ZF2, writing custom view helper in zf2 is very easy thank to zf2 authors. Here in this example i am goanna create StaticFileVersioning Module, you can place this module in two different places , one is vendors directory and other option is module directory.

Even if the topic is old, that solution didn't work for me in ZF2 v2.3.3.

After some research, I found that Navigation Helper is not shared, or it is the twig module that mess up sharing, and if I try to add custom plugin to Navigation (such as a new menu) in module bootstrap, it is just ineffective. But I found an interesting thing in reading Navigation Helper construction in

Zend\Navigation\View\HelperConfig.php

the Navigation plugin manager can be configured in module, global or local config under the key navigation_helpers. That is an easy way to extends Navigation with plugins.

Ex :

module.config.php

'navigation_helpers' => array (
    'invokables' => array(
        'menu' => 'Application\View\Helper\Navigation\Menu',
    ),
),

ZF2 register custom helper for navigation - navigation - iOS, 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� The navigation helper is built in to Zend Framework 2, and uses the service manager configuration we’ve already defined to configure itself automatically. Refreshing your application you will see a working menu, with just a few tweaks however, we can make it look awesome:

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.

That problem is easy enough to solve: just create a role for this user, which inherits from the real roles. See the official docs, and search for the paragraphs referring to the role "someUser", which you should understand as referring to the current user. Hence: the role should be added to the ACL dynamically.

$roles = ['staff', 'editor'];
$this->acl->addRole(new Role('currentuser'), $roles);

Then use "currentuser" as the role to check against.

One issue is that you have to list the "parent roles" in a proper order, i.e. the least privileged roles first, as they're checked in reverse order.

You can try to take care of that by using using something like the following snippet, which will ignore any roles it doesn't recognize, and keep them in the order of the hardcoded list.

# $userRoles is an array with the roles of the current user
$this->acl->addRole(new Role('currentuser'),
    array_intesect(['member', 'staff', 'editor', 'admin'], $userRoles));

View Helpers — Zend Framework 2 documentation, You can also register helpers by adding them to the plugin broker, or the plugin In particular, there are helpers for creating route-based URLs and HTML lists, The navigation helpers are used for rendering navigational elements from� The Navigation helper finds other helpers that implement the Zend\View\Helper\Navigation\Helper interface, which means custom view helpers can also be proxied. This would, however, require that the custom helper path is added to the view. When proxying to other helpers, the Navigation helper can inject its container, ACL/role, and translator

Advanced usage of helpers — Zend Framework 2 2.4.13dev , Writing custom helpers is easy. We recommend Then assume that we register it with the plugin manager, by the name “specialpurpose”. Within a view script,� Placeholder Helper¶. The Placeholder view helper is used to persist content between view scripts and view instances. It also offers some useful features such as aggregating content, capturing view script content for later use, and adding pre- and post-text to content (and custom separators for aggregated content).

Learn Navigation and menus with ZF2, The last step would be to define a factory that can create the Navigation service that will work with this If we extend it, we just need to define our custom menu name on the new factory Menus are rendered using the Navigation view helper . ZF2 register custom helper for navigation; If I register a custom signal handler on a process before forking, will the subsequent child processes also have the custom signal handler registered? Register custom credentials auth provider in ServiceStack; Unable to register A Custom angularjs filter; ASP.Net Core 2.1 register custom ClaimsPrincipal

ZF2: Custom Navigation View Helper, Hi all, Is there a configuration key for setting custom navigation view .com/ questions/15184067/zf2-register-custom-helper-for-navigation The two latter examples can be done in each module that needs to register helpers with the PhpRenderer; however, be aware that another module can register helpers with the same name, so order of modules can impact which helper class will actually be registered!

Comments
  • You should add your user as a role of the acl along with its parents, and then hand that role id to the navigation helper. There's no need for a custom helper.