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 并使所有控制器都扩展该类是个好主意吗?
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01