ZF2,具有多个 if-elseif 的控制器策略设计模式
ZF2, strategy design pattern on controller with multiple if-elseifs
在 ZF2 中,我有实例化控制器的控制器工厂。控制器显示和处理 6 个表单。表单共享相同的 name
提交按钮,但具有不同的提交 value
。
根据表单提交按钮的值,在控制器 indexAction()
中使用多个 if-elseif
处理表单。
我想摆脱 if-elseif
。我想我需要在控制器的工厂上使用策略设计模式 class。问题是我需要以某种方式将提交按钮的值传递给控制器工厂。
我该怎么做?
将其传递给控制器的方法是use Zend\Http\Request;
。使用请求对象,您可以获得发布的值。
但是!挖掘后发现2个资源:
- http://zend-framework-community.634137.n4.nabble.com/ZF2-Injecting-objects-to-a-controller-or-getting-objects-from-the-service-locator-td4656872.html。 @weierophinney(Zend 框架的主要贡献者之一)在这里提出了一些指导方针:
The rule of thumb I have is: more than 5-7 actions in a controller:
refactor. More than 1-2 forms: refactor. Otherwise, the workflow of the
controller becomes too difficult to follow easily.
如果您在控制器中的操作使用相同的表单,您可以设置 setValidationGroup()
并以此根据操作验证表单的不同部分。
因此,对于我的情况,我想我需要重构控制器以分离出表单。
- http://zend-framework-community.634137.n4.nabble.com/zf2-widget-base-app-logic-td4657457.html。这里@weierophinney 建议对表单使用视图助手。
在 ZF2 中,我有实例化控制器的控制器工厂。控制器显示和处理 6 个表单。表单共享相同的 name
提交按钮,但具有不同的提交 value
。
根据表单提交按钮的值,在控制器 indexAction()
中使用多个 if-elseif
处理表单。
我想摆脱 if-elseif
。我想我需要在控制器的工厂上使用策略设计模式 class。问题是我需要以某种方式将提交按钮的值传递给控制器工厂。
我该怎么做?
将其传递给控制器的方法是use Zend\Http\Request;
。使用请求对象,您可以获得发布的值。
但是!挖掘后发现2个资源:
- http://zend-framework-community.634137.n4.nabble.com/ZF2-Injecting-objects-to-a-controller-or-getting-objects-from-the-service-locator-td4656872.html。 @weierophinney(Zend 框架的主要贡献者之一)在这里提出了一些指导方针:
The rule of thumb I have is: more than 5-7 actions in a controller: refactor. More than 1-2 forms: refactor. Otherwise, the workflow of the controller becomes too difficult to follow easily.
如果您在控制器中的操作使用相同的表单,您可以设置 setValidationGroup()
并以此根据操作验证表单的不同部分。
因此,对于我的情况,我想我需要重构控制器以分离出表单。
- http://zend-framework-community.634137.n4.nabble.com/zf2-widget-base-app-logic-td4657457.html。这里@weierophinney 建议对表单使用视图助手。