zf2 Creation of simple service and access it through viewhelper
Asked Answered
S

2

11

I am trying to create a simple service in zf2 which I can access using in viewhelper

Step1. I have craeted a class in src/Application/Service/Service1.php as follow

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()
        {

        }

    }

Step2 I set this up in module.php file as below.

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); 
    });
}

Step3 finally I am geting it in my view helper src/Application/View/Helper/Abc.php test() method like this, I I comment this line $this->sm->get('Application\Service\Service1'); there is no error, there must be something which I am missing in service?

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;

        }
    }

Step4 then I am calling my test view helper in one of view like this.

$this->Abc()->test();

I am getting following error.

Fatal error: Call to undefined method Application\Service\Service1::setView() in vendor/zendframework/zendframework/library/Zend/View/HelperPluginManager.php on line 127 Call Stack:

what am I missing?

Sex answered 24/9, 2012 at 9:48 Comment(4)
Please show some more (real) code. It seems like you're returning the Service1 instance somewhere instead of Abc. Thus, the view helper mechanism tries to inject the view instance into the Service1 instance.Patch
I have changed the name and every reference from Service1 to My1serve but still getting the same error Call to undefined method My1serve::setView(). I have not used the My1serve name anywhere else in my code apart the one which i already showed in question.Sex
Fits perfectly to the problem. We won't be able to help you without a little more information.Patch
No problem, I have find the solution. see the answer below. thanks for your time anyway.Sex
S
5

change the line $this->sm->getServiceLocator()->get('Application\Service\Service1'); in below method

class Abc extends AbstractHelper 
{
   protected $sm;

   public function test()
    {
        $this->sm->getServiceLocator()->get('Application\Service\Service1');
    }
    public function __construct($sm) {
        $this->sm = $sm;

    }
}
Sex answered 24/9, 2012 at 11:36 Comment(0)
D
7

An alternative, in PHP 5.4 only, without specific configuration, would be to use traits:

extract of module.config.php:

'view_helpers' => array(
    'invokables' => array(
        'myHelper' => 'Application\View\Helper\MyHelper',
    ),  

MyHelper.php:

<?php
namespace Application\View\Helper;

use Zend\ServiceManager\ServiceLocatorAwareInterface;  

class HeadScript extends \Zend\View\Helper\MyHelper implements ServiceLocatorAwareInterface
{
    use \Zend\ServiceManager\ServiceLocatorAwareTrait;

    public function __invoke()
    {
        $config = $this->getServiceLocator()->getServiceLocator()->get('Config');
        // do something with retrived config
    }

}
Dissimulation answered 15/2, 2013 at 6:35 Comment(0)
S
5

change the line $this->sm->getServiceLocator()->get('Application\Service\Service1'); in below method

class Abc extends AbstractHelper 
{
   protected $sm;

   public function test()
    {
        $this->sm->getServiceLocator()->get('Application\Service\Service1');
    }
    public function __construct($sm) {
        $this->sm = $sm;

    }
}
Sex answered 24/9, 2012 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.