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 并使所有控制器都扩展该类是个好主意吗?
基础教程推荐
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
