Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question