如何在服务层中注入依赖项?

时间:2017-04-28 08:47:29

标签: php dependency-injection zend-framework2 factory service-layer

我是Zend Framework 2的新手,我是依赖注入,服务层和工厂概念的新手。

我目前正在尝试创建一个需要Service Locator的服务层。一旦它工作,那么我需要将此服务传递给控制器​​,但我不能让它工作。 我总是在PageControllerFactory.php

中收到此错误消息
Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for `Template\Service\PageService` 

有人可以帮我指导我为什么无法为Template\Service\PageService获取或创建实例的正确方向吗?

我在想,也许我没有包含它module.config.php但是不是service_manager factories应该做什么?

module.config.php

'controllers' => array(
    'factories' => array(
        'Template\Controller\Factory\PageControllerFactory',
    ),
),

'service_manager' => array(
    ...
    'factories' => array(
        'Template\Service\Factory\PageServiceFactory',
        'Template\Service\Factory\SharedServiceFactory',
    ),
    ...
),

这是我为SharedService注入服务定位器的工厂

SharedServiceFactory.php

namespace Template\Service\Factory;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Template\Service\SharedService;

class SharedServiceFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        return new SharedService($serviceLocator->getServiceLocator());
    }
}

然后我使用__construct()来检索服务定位器。

SharedService.php

namespace Template\Service;

use Zend\ServiceManager\ServiceLocatorInterface;
use Template\Service\SharedServiceInterface;

class SharedService implements SharedServiceInterface
{
    protected $serviceLocator;
    ...

public function __construct(ServiceLocatorInterface $serviceLocator)
{
    $this->serviceLocator = $serviceLocator;
}
...

此代码在PageControllerFactory.php中断。我认为$parentService->get('Template\Service\PageService')无法在此处检索Template\Service\PageService。这就是它抛出错误的原因。

PageControllerFactory.php

namespace Template\Controller\Factory;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Template\Controller\PageController;

class PageControllerFactory implements FactoryInterface
{
     public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $parentService = $serviceLocator->getServiceLocator();
        $pageService = $parentService->get('Template\Service\PageService');
        $sharedService = $parentService->get('Template\Service\SharedService');

        return new PageController($pageService, $sharedService);
    }
}

PageController.php

namespace Template\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Template\Service\PageServiceInterface;
use Template\Service\SharedServiceInterface;

class PageController extends AbstractActionController
{
    protected $pageService;
    protected $sharedService;

    public function __construct(
        PageServiceInterface $pageService,
        SharedServiceInterface $sharedService
    ) {
        $this->pageService = $pageService;
    }
...

1 个答案:

答案 0 :(得分:1)

而不是:

'service_manager' => array(
    ...
    'factories' => array(
        'Template\Service\Factory\PageServiceFactory',
        'Template\Service\Factory\SharedServiceFactory',
    ),
    ...
),

声明:

'service_manager' => array(
    ...
    'factories' => array(
        'Template\Service\PageService' => 'Template\Service\Factory\PageServiceFactory',
        'Template\Service\SharedService' => 'Template\Service\Factory\SharedServiceFactory',
    ),
    ...
),

配置中的工厂不得以此格式声明: [别名=> [工厂FQCN]]

通过服务定位器,您必须调用别名。