問題描述
我需要所有控制器中的一些通用行為.有一個(gè) BaseController 并使所有控制器都擴(kuò)展該類是個(gè)好主意嗎?如果是這樣,避免 Fatal error: Class 'BaseController' not found
錯(cuò)誤的正確方法是什么.
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.
推薦答案
這當(dāng)然是一種常見的方法.
It's certainly a common approach.
但是一篇文章由采埃孚項(xiàng)目負(fù)責(zé)人 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.
特別是,一個(gè)通用的基本控制器通常會(huì)成為多個(gè)但并非全部控制器中使用的功能的傾倒場(chǎng),因此它變得矯枉過正.動(dòng)作助手是一種延遲加載的替代方案,可在您需要的時(shí)間和地點(diǎn)準(zhǔn)確調(diào)用功能.
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 調(diào)度鉤子 - init()
, preDispatch()
和 postDispatch()
- 允許你自動(dòng)化插件- 基于每個(gè)控制器的類似功能.
Also, the action-helper dispatch hooks - init()
, preDispatch()
and postDispatch()
- allow you to automate plugin-like functionality on a per-controller basis.
我會(huì)去行動(dòng)助手.但與大多數(shù)事情一樣,YMMV.;-)
I'd go action-helpers. But as with most things, YMMV. ;-)
這篇關(guān)于有一個(gè) BaseController 并使所有控制器都擴(kuò)展該類是個(gè)好主意嗎?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!