zf2 Creation of simple service and access it through viewhelper

falpaek

New Member
I am trying to create a simple service in zf2 step1. I have craeted a class in src/XXX/Service/Service1.php as follow\[code\]use Zend\ServiceManager\ServiceLocatorAwareInterface;use Zend\ServiceManager\ServiceLocatorInterface;class Service1 implements ServiceLocatorAwareInterface{ public function __construct() { } public function setServiceLocator(ServiceLocatorInterface $serviceLocator) { } public function getServiceLocator() { }}\[/code\]then I set this up in module.php file as below. \[code\]public function getServiceConfig(){ $module = $this; return array( 'factories' => array( 'XXX\Service\Service1' => function ($sm) { return new \XXX\Service\Service1($sm); }, ) ); }public function onBootstrap($e){ $serviceManager->get('viewhelpermanager')->setFactory('Abc', function ($sm) use ($e) { return new Abc($sm); });}\[/code\]finally I am geting it in my view helper src/XXX/View/Helper/Abc.php test() method like this, I I comment this line \[code\]$this->sm->get('XXX\Service\Service1');\[/code\] there is no error, there must be something which I am missing in service?\[code\]class Abc extends AbstractHelper { protected $sm; public function test() { $this->sm->get('XXX\Service\Service1'); } public function __construct($sm) { $this->sm = $sm; }}\[/code\]then I am calling my test view helper in one of view like this. $this->Abc()->test();I am getting following error. \[code\]Fatal error: Call to undefined method XXX\Service\Service1::setView() in vendor/zendframework/zendframework/library/Zend/View/HelperPluginManager.php on line 127 Call Stack:\[/code\]what am I missing?
 
Top