B
B
bahek24627742014-01-19 17:55:22
Zend Framework
bahek2462774, 2014-01-19 17:55:22

Is it ok to pass a service manager to ViewHelper?

class MyHelper extends AbstractHelper
{
    protected $sm;
    public function __construct($sm) {
        $this->sm = $sm->getServiceLocator();
    }
    public function __invoke() {
         return "some html";
    }
}

а в module.config.php
.....
'view_helpers' => array(
        'factories' => array(
            'MyHelper' => function($sm) {
                return new MyHelper ($sm);
            }
        ),
    ),

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Shirshov Alexander, 2014-01-20
@bahek2462774

If you need a service manager in a helper, then it's better to implement the helper itself from ServiceLocatorAwareInterface .
Whether it's normal or not is debatable, and depends on the context. We try to avoid this.
stackoverflow.com/questions/18075976/injecting-ser...
If a helper needs a model, then it is better to pass a model to it, if a class is needed that is dependent on the service manager, then it is better to resolve these dependencies at the service locator level.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question