zf2 Creación de un servicio simple y acceso a través de viewhelper
Estoy intentando crear un servicio simple en zf2 al cual puedo acceder usando en viewhelper
Paso 1. He craeted una clase en src / Application / Service / Service1.php como sigue
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()
{
}
}
Paso 2 Configuré esto en el archivo module.php como se muestra abajo.
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);
});
}
Paso 3 finalmente lo estoy obteniendo en mi vista helper src / Application / View / Helper / Abc.php método de prueba () como este, yo comento esta línea$this->sm->get('Application\Service\Service1');
no hay error, debe haber algo que me falta en el servicio?
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;
}
}
Etapa 4 entonces estoy llamando a mi asistente de vista de prueba en una vista como esta.
$this->Abc()->test();
Estoy recibiendo error siguiente.
Fatal error: Call to undefined method Application\Service\Service1::setView() in vendor/zendframework/zendframework/library/Zend/View/HelperPluginManager.php on line 127 Call Stack:
¿Qué me estoy perdiendo?