可能重复:
Is a good idea have a BaseController and make all controllers extend that class?从BaseController Zend框架扩展
我想知道的好处/目的,为什么你应该从BaseController扩展您的控制器。我看到有人在做,但我不知道为什么。
可能重复:
Is a good idea have a BaseController and make all controllers extend that class?从BaseController Zend框架扩展
我想知道的好处/目的,为什么你应该从BaseController扩展您的控制器。我看到有人在做,但我不知道为什么。
我在我的第一个ZF项目中从基础控制器扩展而来。在我看来,如果你知道你的所有子类将总是使用超类的相同通用功能,那么这不是一个坏方法。
在任何情况下,对于我的后续项目,我切换到使用动作助手和控制器插件(请参阅Zend Framework: Controller Plugins vs Action Helpers),我从不回头。他们看起来更加灵活,并允许我重用项目中的一些功能。它还坚持更多的“偏好组合继承”的原则(这里有一个相关的问题:Prefer composition over inheritance?)。
希望帮助,
也许你应该在这个看一看:
Extending Zend_Controller_Action
我个人致以控制器一些自定义的功能添加到我的控制器。 示例:如果检查请求是来自cron作业还是普通请求。有时我会检查AJAX请求或禁用调试和分析。
这是我的初始化函数:
public function init() {
parent::init();
if (($this->getRequest()->getParam('type') == 'cron')||($this->getRequest()->getParam('type') == 'cmd') || ($this->getRequest() instanceof Zend_Controller_Request_Http && $this->getRequest()->isXmlHttpRequest())) {
Zend_Registry::get('debug')->disable();
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
}
}
这真的取决于你想在你的控制器做什么。
你也可以使用一个BaseController检查使用preDispatch法授权,即自动实际行动之前称为:
public function preDispatch()
{
$request = Zend_Controller_Front::getInstance()->getRequest();
$controller = $request->getControllerName();
if($controller == 'user') {
return;
}
$auth = Zend_Auth::getInstance();
if (! $auth->hasIdentity()) {
$this->_helper->redirector("login_error", "user");
}
}
@DavidWeinraub不是一个重复。我要求实施这种架构的优点/缺点。 – 2012-01-14 18:21:40