zf2创建简单服务并通过视图助手访问它

11

我正在尝试在zf2中创建一个简单的服务,以便我可以在视图助手中访问

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

        }

    }

第二步 我按照以下方式在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); 
    });
}

第三步 最终我在我的视图助手src/Application/View/Helper/Abc.phptest()方法中获得了它,就像这样,如果我注释掉这一行$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;

        }
    }

第四步,然后我在其中一个视图中像这样调用我的测试视图助手。

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

我遇到了以下错误。

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

我缺少什么?


请展示更多(真实的)代码。看起来你正在返回Service1实例而不是Abc。因此,视图助手机制尝试将视图实例注入到Service1实例中。 - Daniel M
我已经将名称和每个Service1的引用更改为My1serve,但仍然出现相同的错误“Call to undefined method My1serve::setView()”。除了我在问题中已经展示的代码之外,我没有在我的代码中其他地方使用My1serve名称。 - Developer
非常适合这个问题。如果没有更多的信息,我们将无法帮助您。 - Daniel M
没问题,我已经找到了解决方案。请看下面的答案。无论如何感谢您的时间。 - Developer
2个回答

7

在 PHP 5.4 中,不需要特定的配置,另一种选择是使用 traits:

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
    }

}

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;

    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接