zf2创建简单服务并通过viewhelper访问它

Dev*_*per 11 php view-helpers zend-framework2

我试图在zf2中创建一个简单的服务,我可以在viewhelper中使用它

步骤1.我在src/Application/Service/Service1.php中创建了一个类,如下所示

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

        }

    }
Run Code Online (Sandbox Code Playgroud)

Step2我在module.php文件中设置如下.

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); 
    });
}
Run Code Online (Sandbox Code Playgroud)

Step3最后我在我的视图中帮助src/Application/View/Helper/Abc.php测试()方法就像这样,II注释这行$this->sm->get('Application\Service\Service1');没有错误,必须有一些我在服务中缺少的东西?

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;

        }
    }
Run Code Online (Sandbox Code Playgroud)

Step4然后我在这样的视图中调用我的测试视图助手.

$this->Abc()->test();
Run Code Online (Sandbox Code Playgroud)

我收到了以下错误.

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

我错过了什么?

Pow*_*iKi 7

替代方案,仅在PHP 5.4中,没有特定配置,将使用特征:

module.config.php的摘录:

'view_helpers' => array(
    'invokables' => array(
        'myHelper' => 'Application\View\Helper\MyHelper',
    ),  
Run Code Online (Sandbox Code Playgroud)

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
    }

}
Run Code Online (Sandbox Code Playgroud)


Dev*_*per 5

更改$this->sm->getServiceLocator()->get('Application\Service\Service1');以下方法中的行

class Abc extends AbstractHelper 
{
   protected $sm;

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

    }
}
Run Code Online (Sandbox Code Playgroud)