Is a good idea have a BaseController and make all controllers extend that class?(有一个 BaseController 并使所有控制器都扩展该类是个好主意吗?)
问题描述
我需要所有控制器中的一些通用行为.有一个 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. ;-)
这篇关于有一个 BaseController 并使所有控制器都扩展该类是个好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:有一个 BaseController 并使所有控制器都扩展该类是个好主意吗?


基础教程推荐
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01