0

I've made a global variable in bootstrap of Module.php

public function setCashServiceToView($event) {

    $app = $event->getParam('application');
    $cashService = $app->getServiceManager()->get('Calculator/Service/CashServiceInterface');
    $viewModel = $event->getViewModel();
    $viewModel->setVariables(array(
        'cashService' => $cashService,
    ));
}
public function onBootstrap($e) {

    $app = $e->getParam('application');
    $app->getEventManager()->attach(\Zend\Mvc\MvcEvent::EVENT_RENDER, array($this, 'setCashServiceToView'), 100);
}

I can use it inside of my layout.phtml as

$this->cashService;

But I need this variable to use in my partial script of navigation menu, which I call in layout.phtml:

      echo $this->navigation('navigation')
          ->menu()->setPartial('partial/menu')
          ->render();
  ?>

How can I use it inside of my partial/menu.phtml? And may be there is a better way, than to declare it in onBootstrap function?

Thank you for your answers. I decided to make an extended class of \Zend\View\Helper\Navigation\Menu to provide there a property of cashService. However I receive an error:'Zend\View\Helper\Navigation\PluginManager::get was unable to fetch or create an instance for Calculator\Service\CashServiceInterface'. I need this service to display navigation menu. Seems weird, but that's true. I display some diagram in it, using the data, which I get from the service. So why do I have the error? I added to module.config.php

    'navigation_helpers' => array(
    'factories' => array(
        'mainMenu' => 'Calculator\View\Helper\Factory\MainMenuFactory'
    ),

MainMenuFactory:

namespace Calculator\View\Helper\Factory;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Calculator\View\Helper\Model\MainMenu;

Class MainMenuFactory implements FactoryInterface {

    /**
    * Create service
    *
    * @param ServiceLocatorInterface $serviceLocator
    * @return mixed
    */
    public function createService(ServiceLocatorInterface $serviceLocator) {

        return new MainMenu(
            $serviceLocator->get('Calculator\Service\CashServiceInterface')
        );
    }

P.S: CashServiceInterface is an alias to CashServiceFactory

2
  • You could remove the event listener and use a custom view helper to access the service in the view. Commented Dec 4, 2015 at 21:09
  • anyway how can I get it in partial? Commented Dec 4, 2015 at 21:19

1 Answer 1

1

You could remove the event listener and use a custom view helper to access the service in the view.

namespace Calculator\View\Helper;

use Zend\View\Helper\AbstractHelper;

class CashService extends AbstractHelper
{
    protected $cashService;

    public function __construct(CashServiceInterface $cashService)
    {
        $this->cashService = $cashService;
    }

    public function __invoke()
    {
        return $this->cashService;
    }
}

Create a factory.

namespace Calculator\View\Helper; 

class CashServiceFactory
{
    public function __invoke($viewPluginManager)
    {
        $serviceManager = $viewPluginManager->getServiceLocator();
        $cashService    = $serviceManager->get('Calculator\\Service\\CashServiceInterface');

        return new CashService($cashService);
    }
}

Register the new helper in moudle.config.php.

'view_helpers' => [
    'factories' => [
        'CashService' => 'Calculator\View\Helper\CashServiceFactory',
    ],
],

Then you can use the plugin in all view scripts.

$cashService = $this->cashService();
Sign up to request clarification or add additional context in comments.

4 Comments

So if I create a custom view helper like that, I can than so simply use it even in partial/menu?
So as far as I understand, you are proposing me to make a viewHelper wrapper around my cashService? Is it a good practice and the only one solution? Are there any other ways to supply my partial/menu with parameters?
Yes, correct. I'm not saying this is the only way; it is however the intended purpose of the view helpers. As I understand it, your service is not a variable of the view; it represents view/business logic, it's a service. By appending it as a variable to the view model is also a performance overhead if you're not using it on each request. ZF2 allows you to encapsulate complex logic using view helpers specifically for this purpose. As my example shows, you can inject and reuse services from the service manager.
thank you AlexP, I edited my question and decided to create an extension for Menu, can you please take a look why am I getting an error?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.