有一个 BaseController 并使所有控制器都扩展该类是个好主意吗?

2021-12-29 00:00:00 php zend-framework classnotfound

我需要所有控制器中的一些通用行为.有一个 BaseController 并使所有控制器都扩展该类是个好主意吗?如果是这样,避免 Fatal error: Class 'BaseController' not found 错误的正确方法是什么.

I need some common behaviors in all controllers. Is a good idea have a BaseController and make all controllers extend that class? If so, What is the correct way to avoid the Fatal error: Class 'BaseController' not found error.

推荐答案

这当然是一种常见的方法.

It's certainly a common approach.

但是一篇文章由采埃孚项目负责人 Matthew Weier O'Phinney 撰写 解释了 action-helpers 如何以更大的灵活性提供相同的好处.

But an article by ZF project lead Matthew Weier O'Phinney explains how action-helpers can provide the same benefits with more flexibility.

特别是,一个通用的基本控制器通常会成为多个但并非全部控制器中使用的功能的倾倒场,因此它变得矫枉过正.动作助手是一种延迟加载的替代方案,可在您需要的时间和地点准确调用功能.

In particular, a common base controller often becomes a dumping ground for functionality that is used in multiple-but-not-all controllers, so it become overkill. Action-helpers are kind of a lazy-load alternative, invoking functionality precisely when and where you need it.

此外,action-helper 调度钩子 - init(), preDispatch()postDispatch() - 允许你自动化插件- 基于每个控制器的类似功能.

Also, the action-helper dispatch hooks - init(), preDispatch() and postDispatch() - allow you to automate plugin-like functionality on a per-controller basis.

我会去行动助手.但与大多数事情一样,YMMV.;-)

I'd go action-helpers. But as with most things, YMMV. ;-)

相关文章