Zend 框架 2:了解插件和服务

Zend framework 2: Understanding plugins & services

我使用 Zend Framework 2 已有一段时间了。尽管如此,我还是不太了解它的结构和组件。这样做的原因很可能是我对 PHP 和相关模式的一般了解。

特别是:

ServiceLocator

The basic idea behind a service locator is to have an object that knows how to get hold of all of the services that an application might need. […] With a Service Locator every user of a service has a dependency to the locator. The locator can hide dependencies to other implementations, but you do need to see the locator.

ZF2 PluginManager 是一种 ServiceLocator。它知道如何创建控制器插件并控制它们的生命周期。控制器依赖于管理器。

Service Layer

Defines an application's boundary with a layer of services that establishes a set of available operations and coordinates the application's response in each operation.

ServiceLayers 通常 类 没有自己的状态。他们只协调其他 类。边界将某些用例封装在您的应用程序中。详情请参考http://martinfowler.com/bliki/BoundedContext.html.

Plugin

Links classes during configuration rather than compilation. […] Configuration shouldn't be scattered throughout your application, nor should it require a rebuild or redeployment. Plugin solves both problems by providing centralized, runtime configuration.

ZF Controller Plugins 允许您分离各个关注点,例如 getting the Identity of a User, accessing Params from the Request, setting a Flash Message into discrete units. Having these helps with Separation of Concerns and prevents God Controllers。简而言之:如果您有需要在控制器之间共享的代码,请将其放入插件中。