0

我的应用程序是 POPO 的集合,我正在尝试使用 Zend Framework 2 Service Manager 连接这些 POPO。

为了说明我的问题,举个例子:

$config = array(
   'services' => array(
      'my.app.serviceA' => new \My\App\Services\ServiceA(),
      'my.app.serviceB' => new \My\App\Services\ServiceB(),

      'my.app.manager.task' => new \My\App\Manager\TaskManager(),
   ),

);

我的 TaskManager 类看起来像这样:

class TaskManager {

   protected $serviceA;
   protected $serviceB;

   public function setServiceA( \My\App\Service\ServiceA $serviceA )
   {
      $this->serviceA = $serviceA;
   }

   public function setServiceB( \My\App\Service\ServiceB $serviceB )
   {
      $this->serviceB = $serviceB;
   }

}

如您所见,TaskManager该类同时依赖于ServiceAServiceB。如何使用为和my.app.manager.task定义的服务名称将这些服务注入到使用 Service Manager 配置中?ServiceAServiceB

更新:

我开始相信我根本不应该将 ServiceManager 组件用于我的目的,而应该使用 Zend DI 组件。

我的印象是 ServiceManager 是一个 ZF2“框架”组件,而 Zend\DI 似乎更像是一个通用的通用 DiC。因此,这可能是 ServiceManager 与 MVC 和 ModuleManager 组件(它们似乎也是“框架”组件)绑定关系的原因。

也许有人可以澄清一下?

4

3 回答 3

2

在 module.config.php 服务管理器可以通过 7 种不同的方式进行配置:

return array(

    // instantiate the class for you when needed
    'invokables' => array(
        'commentController' => '\Comment\Controller\CommentController';
    ),

    // Aliasing a name to a known service name
    'aliases' => array(
        'Comment\Service' => 'commentService';
    ),

    // configure the instance of the object
    'factories' => array(
        'commentController' => function ($sm) {
            $locator = $sm->getServiceLocator();
            $controller = $locator->get('commentController');
            $controller->setCommentService($locator->get('Comment\Service'));
            return $controller;
        }
    ),

    // register already instantiated objects
    'services' => array(
        'commentController' => new \Comment\Controller\CommentController(),
    ),

    //factory instance that can create multiple services based on the name supplied to the factory.
    'abstract_factories' => array(
        'SomeModule\Service\FallbackFactory',
    ),

    // initialize the service whenever service created
    'initializers' => array(
        function ($instance, $sm) {
            if ($instance instanceof \Comment\Controller\CommentController){
                $instance->setCommentService($sm->get('Comment\Service'));
            }
        }
    ),

    // indicating whether or not a service should be shared
    'shared' => array(
        'commentController' => false,
    ),
);

在 Module.php 中

public function getControllerConfig() {
    return array(
        'factories' => array(
            'commentController' => function ($sm) {
                $controller = new \Comment\Controller\CommentController();
                $locator = $sm->getServiceLocator();
                $controller->setCommentForm($locator->get('commentForm'));
                $controller->setCommentService($locator->get('commentService'));
                return $controller;
            }
        )
    );
}

在控制器中简单使用:

 $commentService = $this->serviceLocator->get('Comment\Service');

你把它放在 getter 或 init() 方法 ZF2 的 New Controller::init() :: phly, boy, phly

于 2014-03-15T06:02:01.883 回答
0

在控制器中;

$yourService = $this->getServiceLocator()->get('your_service_alias');

在 View Helper 中:您应该通过 viewHelper 的构造函数从 Module.php 发送

    public function getViewHelperConfig() {
        return array(
            'factories' => array(
                'loginHelper' => function($sm) {
                    return new LoginHelper($sm);
                }
        )
   }

在一堂课

use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
    public class UseCaseBO implements ServiceLocatorAwareInterface {
      protected $serviceLocator;

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

      public function getServiceLocator() {
        return $this->serviceLocator;
      }

      // now instance of Service Locator is ready to use
      public function someMethod() {
        $table = $this->serviceLocator->get('your_service_alias');
        //...
      }
    }
于 2014-03-15T09:29:19.813 回答
0

对我来说,最好的方法是创建一个类工厂并使用 factoryInterface,如下所示:

 return array(

    'service_manager' => array(
        'factories' => [
            'Task' => 'Application\TaskFactory',
        ],
        'invokables' => array(
            'Task'=> 'Application\Task',
            'ServiceA'=> 'Application\ServiceA',
            'ServiceB' => 'Application\ServiceB'
        )
    ),
);

还有一个工厂类:

class TaskFactory implements FactoryInterface {

    /** @var  ServiceLocatorInterface $serviceLocator */
    var $serviceLocator;

    public function createService(ServiceLocatorInterface $serviceLocator) {
        $sl = $this->serviceLocator = $serviceLocator;

        // you can get your registered services
        $serviceA = $sl->get('ServiceA');
        $serviceB = $sl->get('ServiceB');


        // You can build your class using the class loader
        $task = new Application\Task();

        // Or the Service Locator Again
        $task = $sl->get('Task');

        return $task;
    }
}

您可以在 Task 类上实现工厂接口。我更喜欢控制我正在构建的内容。

于 2014-03-15T16:09:18.810 回答