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


我试图在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()
        {

        }

    }

第2步 我在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.php测试()方法这样,我评论这一行 $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;

        }
    }

步骤4 然后我在这样的视图中调用我的测试视图助手。

$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:

我错过了什么?


1042
2017-09-24 09:48


起源

请显示更多(真实)代码。好像你要回来了 Service1 实例在某处而不是 Abc。因此,视图助手机制尝试将视图实例注入到 Service1 实例。 - Daniel M
我已经将名称和每个引用从Service1更改为My1serve但仍然得到相同的错误调用未定义的方法My1serve :: setView()。我没有在我的代码中的任何其他位置使用My1serve名称,除了我已经显示的那个。 - Developer
完全适合这个问题。如果没有更多信息,我们将无法为您提供帮助。 - Daniel M
没问题,我找到了解决方案。看到下面的答案。不管怎样,谢谢你的时间。 - Developer


答案:


在PHP 5.4中,没有特定配置的替代方案是使用 性状

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
    }

}

7
2018-02-15 06:35





换线 $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;

    }
}

5
2017-09-24 11:36