Zend framework 2: service locator in the field of view of the assistant

I am trying to access the service locator in a view helper so that I can access my configuration. I use this view helper for a recursive function, so I don’t know where to declare the service locator.

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;
use CatMgt\Model\CategoryTable as RecursiveTable;

class CategoryRecursiveViewHelper extends AbstractHelper
{
    protected $table;

    public function __construct(RecursiveTable $rec)
    {
        $this->table = $rec; 
    }

    public function __invoke($project_id, $id, $user_themes_forbidden, $level, $d, $role_level)
    {

       $config = $serviceLocator->getServiceLocator()->get('config');

       //So i can access $config['templates']

       $this->__invoke($val->project_id, $id, $user_themes_forbidden, $level, $d, $role_level);

    }

}

I tried the solution provided here link

But this did not help, is it right to do?

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;
use CatMgt\Model\CategoryTable as RecursiveTable;
use Zend\View\HelperPluginManager as ServiceManager;

class CategoryRecursiveViewHelper extends AbstractHelper
{
    protected $table;
    protected $serviceManager;

    public function __construct(RecursiveTable $rec, ServiceManager $serviceManager)
    {
        $this->table = $rec; 
        $this->serviceManager = $serviceManager;
    }

    public function __invoke($project_id, $id, $user_themes_forbidden, $level, $d, $role_level)
    {

       $config = $this->serviceManager->getServiceLocator()->get('config');

       //So i can access $config['templates']

       $this->__invoke($val->project_id, $id, $user_themes_forbidden, $level, $d, $role_level);

    }

}
+4
source share
1 answer

First of all, yours ViewHelperis an endless loop, and your application will crash. You call __invokewithin __invoke- it just can't work.

Register ViewHelper with dependencies

-, ViewHelper :

class FooBarHelper extends AbstractHelper
{
    protected $foo;
    protected $bar;

    public function __construct(Foo $foo, Bar $bar)
    {
        $this->foo = $foo;
        $this->bar = $bar;
    }

    public function __invoke($args)
    {
        return $this->foo(
            $this->bar($args['something'])
        );
    }
}

ViewHelper. , factory.

// module.config.php
'view_helpers' => [
    'factories' => [
        'foobar' => 'My\Something\FooBarHelperFactory'
    ]
]

- factory, . :

class FooBarHelperFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $sl)
    {
        // $sl is instanceof ViewHelperManager, we need the real SL though
        $rsl = $sl->getServiceLocator();
        $foo = $rsl->get('foo');
        $bar = $rsl->get('bar');

        return new FooBarHelper($foo, $bar);
    }
}

ViewHelper $this->foobar($args) .

ServiceLocator AS

, ServiceManager , . , . , , __construct() , ServiceManager.

+15

Source: https://habr.com/ru/post/1539683/


All Articles