Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

php - zf2 Creation of simple service and access it through viewhelper

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 ApplicationService;
    use ZendServiceManagerServiceLocatorAwareInterface;
    use ZendServiceManagerServiceLocatorInterface;

    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(
            'ApplicationServiceService1' => function ($sm) {
                return new ApplicationServiceService1($sm);
            },
        )
    );   
}

public function onBootstrap($e)
{        
   $serviceManager = $e->getApplication()->getServiceManager();

    $serviceManager->get('viewhelpermanager')->setFactory('Abc', function ($sm) use ($e) {
        return new ApplicationViewHelperAbc($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('ApplicationServiceService1'); there is no error, there must be something which I am missing in service?

namespace ApplicationViewHelper;

use ZendViewHelperAbstractHelper;

    class Abc extends AbstractHelper 
    {
       protected $sm;

       public function test()
        {
            $this->sm->get('ApplicationServiceService1');
        }
        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 ApplicationServiceService1::setView() in vendor/zendframework/zendframework/library/Zend/View/HelperPluginManager.php on line 127 Call Stack:

what am I missing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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' => 'ApplicationViewHelperMyHelper',
    ),  

MyHelper.php:

<?php
namespace ApplicationViewHelper;

use ZendServiceManagerServiceLocatorAwareInterface;  

class HeadScript extends endViewHelperMyHelper implements ServiceLocatorAwareInterface
{
    use endServiceManagerServiceLocatorAwareTrait;

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...