Zend Framework 2 MVC应用程序中的依赖关系管理 [英] Dependency management in Zend Framework 2 MVC applications

查看:120
本文介绍了Zend Framework 2 MVC应用程序中的依赖关系管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于ServiceLocatorAwareInterface可能会被从ZF3中的AbstractController中删除,因此相关性应该而是通过构造函数或setter方法传递.

As the ServiceLocatorAwareInterface will likely be removed from the AbstractController in ZF3, dependencies should instead be passed via the constructor or via setter methods.

请牢记这一点,请考虑用户或站点控制器的用例,该用例具有诸如注册,激活帐户,登录,注销等操作.至少,这将需要UserService和2种形式.再添加一些其他相关操作(远程身份验证,帐户链接等),您最终将获得4或5种形式.

With this in mind, consider the use case of a user or site controller with actions such as register, activate account, login, logout, etc. At a minimum, this would require a UserService and 2 forms. Add a few more related actions (remote authentication, linking of accounts, etc.) and you end up with 4 or 5 forms.

通过构造函数传递所有这些依赖关系充其量只能是一团糟,更重要的是,每个动作通常只需要一种形式.

Passing all these dependencies via the constructor would be messy at best, and more importantly, only 1 form is usually required per action.

您认为以下哪种技术更好?为什么?

Which one of the following techniques do you think is better, and why?

  1. 为每个操作创建单独的控制器,以便每个控制器仅需要一种形式(除了服务).例如RegistrationController,LoginController,LinkAccountController等.

  1. Create separate controllers for each action, so that each controller will only require a single form (in addition to a service). For example RegistrationController, LoginController, LinkAccountController, etc.

  • 您最终会以这种方式获得许多控制器.

在控制器的工厂中,根据请求的动作提供不同的表格.

In the factory for the controller, supply different forms based on which action is being requested.

  • 控制器的构造变得依赖于此工厂,更具体地说,是依赖于请求环境(路由等).您可以直接构造控制器(用于测试或进行其他操作),但随后需要确保依赖关系可用并抛出异常.

使用事件管理器,在需要表单时在控制器中触发事件,并让事件处理程序按需提供依赖项.

Use the event manager, trigger an event in the controller when a form is required, and let an event handler supply the dependency on demand.

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆