关于官网手册
默认情况下,利用Zend框架内的Zend\ ServiceManager的MVC三层。因此,在大多数情况下,你会被提供服务,可调用的类,别名,和工厂,可以通过配置或通过你的模块类。
默认情况下,模块管理器监听的Zend\ ModuleManager会\监听\的ServiceListener将执行以下操作:
对于modules实现的Zend\ ModuleManager\feature\ ServiceProvider接口,或getServiceConfig()的方法,它会调用该方法,并合并配置。
已处理所有模块后,它会抢的Zend\registered\ModuleManager会\feature\ ConfigListener“的配置,并合并的任何配置下service_manager键。
最后,它将使用合并的配置,配置的ServiceManager。
在大多数情况下,你不会与ServiceManager的互动,比其他提供服务,您的应用程序通常会ServiceManager的依赖于良好的配置,以确保正确配置它们的依赖类。然而,当创建工厂,你可能要与ServiceManager的互动,检索等服务的依赖注入。此外,也有一些情况下,你可能想收到ServiceManager的懒惰检索依赖,正因为如此,你会想实施ServiceLocatorAwareInterface,学习API的ServiceManager。
使用配置¶
配置需要service_manager的键的配置在顶层,与一个或多个下面的子键:
abstract_factories,这应该是一个数组抽象工厂类的名称。
aliases,这应该是一个关联数组,别名/目标名称对(目标名称也可能是一个别名)。
factories,服务名称/工厂类名称对一个数组。工厂应该是实现的Zend\feature ServiceManager\feature \FactoryInterface或可调用类的类。如果您使用的是PHP的配置文件,您可以为工厂提供任何PHP调用,匿名函数。
invokables,服务名称/类别名称对一个数组。类名应该是没有任何构造函数的参数,可以直接实例化的类。
services,服务名称/对象对一个数组。显然,这将只与PHP配置。
shared 服务名称/布尔对的数组,指示是否应该是共享的服务。 ServiceManager的假设默认情况下,所有的服务都共享,但你可以指定一个布尔值false值指示应该返回一个新的实例。
使用配置¶
作为服务提供商的模块¶
模块可以作为服务配置提供。要做到这一点,模块类必须实现的Zend\feature ModuleManager\feature \ServiceProviderInterface中的或getServiceConfig()的方法(这也是在接口中定义)。此方法必须返回下列操作之一:
一个数组(或Traversable对象)的配置与Zend\feature ServiceManager\feature配置兼容。 (基本上,它应该有keys上面所讨论的配置。
一个字符串提供实现的Zend\feature ServiceManager\feature ConfigInterface一类的名称。
要么Zend\feature ServiceManager\feature Config中的一个实例,或对象实现的Zend\feature ServiceManager\feature ConfigInterface。
如前所述,这样的配置将被合并,从其他模块以及配置文件,返回之前被传递到ServiceManager配置,这使得容易重写modules配置。
例子¶
示例配置¶
以下是被合并在你的应用程序的任何配置的有效配置,并演示了每个可能的配置项。配置合并按以下顺序:
配置通过getServiceConfig()方法,在处理中的模块顺序从模块类返回。
配置下service_manager键模块,modules的顺序进行处理。
应用程序的config /autoload/目录下的配置,它们被处理的顺序。
首先是配置:
可以配置在module/modulename/config/module.config.php
如下:
<?php // a module configuration, "module/SomeModule/config/module.config.php" return array( 'service_manager' => array( 'abstract_factories' => array( // Valid values include names of classes implementing 实现abstractfactoryinterface接口的类名,或类的实例或者 匿名函数 // AbstractFactoryInterface, instances of classes implementing // AbstractFactoryInterface, or any PHP callbacks 'SomeModule\Service\FallbackFactory', ), 'aliases' => array( // Aliasing a FQCN to a service name 给一个类 设置别名 'SomeModule\Model\User' => 'User', // Aliasing a name to a known service name 已经定义的设置设置别名 'AdminUser' => 'User', // Aliasing to an alias 别名设置别名 'SuperUser' => 'AdminUser', ), 'factories' => array( // Keys are the service names. 键名是服务名,get(“键名”) // Valid values include names of classes implementing // FactoryInterface, instances of classes implementing // FactoryInterface, or any PHP callbacks 值是实现了factoryinterface接口的类名或者实例,或者匿名函数。 'User' => 'SomeModule\Service\UserFactory', 'UserForm' => function ($serviceManager) { $form = new SomeModule\Form\User(); // Retrieve a dependency from the service manager and inject it! $form->setInputFilter($serviceManager->get('UserInputFilter')); return $form; }, ), 'invokables' => array( // Keys are the service names get("name") // Values are valid class names to instantiate. 类名 'UserInputFiler' => 'SomeModule\InputFilter\User', ), 'services' => array( // Keys are the service names // Values are objects 'Auth' => new SomeModule\Authentication\AuthenticationService(), ), 'shared' => array( // Usually, you'll only indicate services that should _NOT_ be // shared -- i.e., ones where you want a different instance // every time. 'UserForm' => false, ), ), ); 注意 配置和PHP 通常情况下,你不应该有你的配置文件,创建新的实例对象,甚至关闭工厂,在配置时,不是所有的自动加载可能,如果其他配置覆盖此之后,你现在花费CPU内存进行工作,最终失去。 要求工厂的实例,写一个工厂。如果你想添加特定的的配置实例,使用Module类的这样做,或者一个侦听器。 此外,你就不能使用的配置文件缓存功能,当您使用内封。这是一个PHP限制不能的(de)连载关闭。 也可以先创建一个serviceConfig的类,继承Zend\ServiceManager\Config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
namespace SomeModule\Service;
use SomeModule\Authentication;
use SomeModule\Form;
use Zend\ServiceManager\Config;
use Zend\ServiceManager\ServiceManager;
class ServiceConfiguration extends Config
{
/**
* This is hard-coded for brevity.这里是是硬编码的。。
*/
public function configureServiceManager(ServiceManager $serviceManager)
{
$serviceManager->setFactory('User', 'SomeModule\Service\UserFactory');
$serviceManager->setFactory('UserForm', function ($serviceManager) {
$form = new Form\User();
// Retrieve a dependency from the service manager and inject it!
$form->setInputFilter($serviceManager->get('UserInputFilter'));
return $form;
});
$serviceManager->setInvokableClass('UserInputFilter', 'SomeModule\InputFilter\User');
$serviceManager->setService('Auth', new Authentication\AuthenticationService());
$serviceManager->setAlias('SomeModule\Model\User', 'User');
$serviceManager->setAlias('AdminUser', 'User');
$serviceManager->setAlias('SuperUser', 'AdminUser');
$serviceManager->setShared('UserForm', false);
}
}
|
Now, we’ll consume it from our Module.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
namespace SomeModule;
// We could implement Zend\ModuleManager\Feature\ServiceProviderInterface.
// However, the module manager will still find the method without doing so.
class Module
{
public function getServiceConfig()
{
return new Service\ServiceConfiguration();
// OR:
// return 'SomeModule\Service\ServiceConfiguration';
}
}
|
或者直接在Module.php里面写上
Module Returning an Array
The following demonstrates returning an array of configuration from a module class. It can be substantively the same asthe array configuration from the previous example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
namespace SomeModule;
class Module
{
public function getServiceConfig()
{
return array(
'abstract_factories' => array(),
'aliases' => array(),
'factories' => array(),
'invokables' => array(),
'services' => array(),
'shared' => array(),
);
}
}
|
默认情况下,Zend Framework的MVC寄存器的初始化,
将 实现了Zend\ServiceManager\ServiceLocatorInterface的 ServiceManager实例 添加到任何类实现的Zend\ServiceManager\ServiceLocatorAwareInterface的类
。一个简单的实现如下所示。
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
namespace SomeModule\Controller\BareController;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Stdlib\DispatchableInterface as Dispatchable;
use Zend\Stdlib\RequestInterface as Request;
use Zend\Stdlib\ResponseInterface as Response;
class BareController implements
Dispatchable,
ServiceLocatorAwareInterface
{
protected $services;
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->services = $serviceLocator;
}
public function getServiceLocator()
{
return $this->services;
}
public function dispatch(Request $request, Response $response = null)
{
// ...
// Retrieve something from the service manager
$router = $this->getServiceLocator()->get('Router');
// ...
}
}
|