問題描述
我需要所有控制器中的一些通用行為.有一個 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 并使所有控制器都擴展該類是個好主意嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!