Zend 框架 2:了解插件和服务
Zend framework 2: Understanding plugins & services
我使用 Zend Framework 2 已有一段时间了。尽管如此,我还是不太了解它的结构和组件。这样做的原因很可能是我对 PHP 和相关模式的一般了解。
特别是:
什么是服务?
我知道框架的核心设计模式之一是服务定位器模式,但我还没有真正掌握什么是服务。另外,人们谈论 "implementing a service layer" 是什么意思?
什么是插件?
我认为在 Zend Framework 2 中,正确的术语是 "controller plugin"。据我了解,它可能只是一个包装器,一个 class,它提供了一个很好的 api 供在控制器中使用。
示例:与其一次又一次地调用相同的 10 行代码,不如将它们包装在一个控制器插件中,该插件包含一个封装这 10 行代码的方法,因此可以通过一次方法调用完成同样的事情。这是正确的吗?
如果是对的:为什么要把代码封装在插件里,不加插件直接加到controller里不行吗?
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。它知道如何创建控制器插件并控制它们的生命周期。控制器依赖于管理器。
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.
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。简而言之:如果您有需要在控制器之间共享的代码,请将其放入插件中。
我使用 Zend Framework 2 已有一段时间了。尽管如此,我还是不太了解它的结构和组件。这样做的原因很可能是我对 PHP 和相关模式的一般了解。
特别是:
什么是服务?
我知道框架的核心设计模式之一是服务定位器模式,但我还没有真正掌握什么是服务。另外,人们谈论 "implementing a service layer" 是什么意思?什么是插件?
我认为在 Zend Framework 2 中,正确的术语是 "controller plugin"。据我了解,它可能只是一个包装器,一个 class,它提供了一个很好的 api 供在控制器中使用。
示例:与其一次又一次地调用相同的 10 行代码,不如将它们包装在一个控制器插件中,该插件包含一个封装这 10 行代码的方法,因此可以通过一次方法调用完成同样的事情。这是正确的吗?
如果是对的:为什么要把代码封装在插件里,不加插件直接加到controller里不行吗?
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。它知道如何创建控制器插件并控制它们的生命周期。控制器依赖于管理器。
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.
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。简而言之:如果您有需要在控制器之间共享的代码,请将其放入插件中。