zf2 Criação de serviço simples e acesso através do viewhelper
Eu estou tentando criar um serviço simples no zf2 que eu possa acessar usando no viewhelper
Passo 1. Eu craeton uma classe em src / Application / Service / Service1.php como segue
namespace Application\Service;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class Service1 implements ServiceLocatorAwareInterface
{
public function __construct()
{
}
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
}
public function getServiceLocator()
{
}
}
Passo 2 Eu configurei isso no arquivo module.php como abaixo.
public function getServiceConfig()
{
return array(
'factories' => array(
'Application\Service\Service1' => function ($sm) {
return new \Application\Service\Service1($sm);
},
)
);
}
public function onBootstrap($e)
{
$serviceManager = $e->getApplication()->getServiceManager();
$serviceManager->get('viewhelpermanager')->setFactory('Abc', function ($sm) use ($e) {
return new \Application\View\Helper\Abc($sm);
});
}
Etapa 3 finalmente estou recebendo na minha opinião auxiliar src / Application / View / Helper / Abc.php teste () método como este, eu comento esta linha$this->sm->get('Application\Service\Service1');
não há erro, deve haver algo que estou perdendo em serviço?
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
class Abc extends AbstractHelper
{
protected $sm;
public function test()
{
$this->sm->get('Application\Service\Service1');
}
public function __construct($sm) {
$this->sm = $sm;
}
}
Passo 4 então eu estou chamando meu assistente de visualização de teste em uma visão como essa.
$this->Abc()->test();
Eu estou recebendo o seguinte erro.
Fatal error: Call to undefined method Application\Service\Service1::setView() in vendor/zendframework/zendframework/library/Zend/View/HelperPluginManager.php on line 127 Call Stack:
o que estou perdendo?