Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Documentation seems to be wrong ... #55

Open
@ghost

Description

I found this code snippet in den documenation for the zend-navigation component (ZF3):

Docs » Reference » View Helpers » Intro

The Code snippet provided is not working for me:

<?php
// module/MyModule/Module.php

namespace MyModule;

use Zend\View\HelperPluginManager;
use Zend\Permissions\Acl\Acl;
use Zend\Permissions\Acl\Role\GenericRole;
use Zend\Permissions\Acl\Resource\GenericResource;

class Module
{
    /* ... */
    public function getViewHelperConfig()
    {
        return [
            'factories' => [
                // This will overwrite the native navigation helper
                'navigation' => function(HelperPluginManager $pm) {
                    // Setup ACL:
                    $acl = new Acl();
                    $acl->addRole(new GenericRole('member'));
                    $acl->addRole(new GenericRole('admin'));
                    $acl->addResource(new GenericResource('mvc:admin'));
                    $acl->addResource(new GenericResource('mvc:community.account'));
                    $acl->allow('member', 'mvc:community.account');
                    $acl->allow('admin', null);

                    // Get an instance of the proxy helper
                    $navigation = $pm->get('Zend\View\Helper\Navigation');

                    // Store ACL and role in the proxy helper:
                    $navigation->setAcl($acl);
                    $navigation->setRole('member');

                    // Return the new navigation helper instance
                    return $navigation;
                }
            ]
        ];
    }
    /* ... */
}

First it seems that in ZF3 the HelperPluginManager is not passed to the closure. Instead, I found out that the parent ServiceManager is passed.

If I change it to the code below, then the example is working for me:

class Module
{
    /* ... */
    public function getViewHelperConfig()
    {
        return [
            'factories' => [
                // This will overwrite the native navigation helper
                Helper\Navigation::class => function(\Zend\ServiceManager\ServiceLocatorInterface $serviceManager)
                {
                    $navigationViewHelper = new \Zend\View\Helper\Navigation();
                    $navigationViewHelper->setServiceLocator($serviceManager);

                    // Store ACL and role in the proxy helper:
                    $navigationViewHelper->setAcl($this->acl);
                    $navigationViewHelper->setRole($this->role);

                    // Return the new navigation helper instance
                    return $navigationViewHelper;
                }
            ]
        ];
    }
    /* ... */
}

Am I right? Is the documentation for ZF3 not correct?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions