How to inject non-acl object into navigation?

Hi,

I want to know how to inject non-acl into the navigation helper. The answer given in this post is for injecting ACL into the navigation helper via the delegator.

I’m able to somehow achieve my goal by the answer given in this post. But asking for a better solution. Thanks!

A delegator allows you to extend the desired service using the laminas-servicemanager. Because the service manager already exists in a laminas-mvc based application, therefore it is an easy way without creating your own navigation helper or overwriting an existing one.

Or you can explain what you would like to achieve in concrete terms, and maybe there will be another way.

Hi @froschdesign,

I did achieve my goal by extending the desired service in my case it is the navigation menu helper, which was achieved with your help in this post. So the way I did it is by adding my method in the menu helper called setAuthorizationService(). Then to access the authorization service in the view, I created another helper to access my authorization service. So the code looks something like below.

$this->navigation('navigation')
         ->bootstrapMenu()
         ->setAuthorizationService($this->myAuthorizationService());
/*
 * Below method returns the LmcRbac/RouteGuard
 * $this->myAuthorizationService()
*/

This is the way I’ve done it. I want to know if this is the right way. Thanks!

I have another idea for you: decorate the navigation container, not the view helper.

Check the access at the container and remove any pages that are not allowed or make them invisible. The navigation container can be retrieved from the service container of your application.
This is also what the helpers do:

And a delegator can help here too.

The access does not need to be checked at the view layer.

Hi @froschdesign,

I didn’t get your idea of decorating the navigation container. As it can’t access the accept method of the view helper. My view helper at the moment is able to handle any navigation configuration. At the moment my view helper can be used like below.

$this->navigation('navigation')
    ->bootstrapMenu()
    ->setAuthorizationService($this->myAuthorizationService());

$this->navigation('admin_navigation')
    ->bootstrapMenu()
    ->setAuthorizationService($this->myAuthorizationService());

$this->navigation('discourse_navigation')
    ->bootstrapMenu()
    ->setAuthorizationService($this->myAuthorizationService());

use Laminas\Navigation\Navigation;

$pages = [
    [
        'label'      => 'Home',
        'title'      => 'Go Home',
        'uri'      => $this->serverUrl(),
        'resource' => 'home', // route name
        'order'      => -100, // make sure home is the first page
    ],
    [
        'label'      => 'About',
        'title'      => 'Go to about',
        'resource' => 'about',
        'uri'      => $this->serverUrl(),
        
    ],
];
$onDemandContainer = new Navigation($pages);
$this->navigation('navigation')
    ->setContainer($onDemandContainer)
    ->bootstrapMenu()
    ->setAuthorizationService($this->myAuthorizationService());

So, I didn’t get your idea about decorating the container and where to look at it. As for your decorator option, a hint would be great. Thanks!