从 Zend Framework 中的 BaseController 扩展

发布于 2024-12-24 17:00:08 字数 311 浏览 3 评论 0原文

可能的重复:
是个好主意有一个 BaseController 并使所有控制器都扩展该类?

我想知道为什么应该从 BaseController 扩展控制器的好处/目的。我看到有人这样做,但我不知道为什么。

Possible Duplicate:
Is a good idea have a BaseController and make all controllers extend that class?

I would like to know the benefits/purposes why you should extend your controllers from a BaseController. I see people doing it, but I just don't know why.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷默言语 2024-12-31 17:00:08

我在第一个 ZF 项目中从基础控制器进行了扩展。在我看来,如果您知道所有子类实际上总是使用超类的相同通用功能,那么这不是一个坏方法。

无论如何,对于我的以下项目,我转而使用操作助手和控制器插件(请参阅Zend Framework:控制器插件与操作助手),我再也没有回头。它们对我来说看起来更加灵活,并且允许我在项目之间重用一些功能。它还更多地遵循“优先组合而不是继承”原则(这里有一个相关问题:优先组合而不是继承?)。

希望有帮助,

I extended from a base controller in my first ZF project. In my opinion, it's not a bad approach if you know that all your subclasses will always actually use the same common functionality of the superclass.

In any case, for my following projects, I switched to using Action Helpers and Controller Plugins (see Zend Framework: Controller Plugins vs Action Helpers), and I never looked back. They look much more flexible to me, and allow me to reuse some features from project to project. It also adheres more to the "Prefer composition over inheritance" principle (there's a related question here: Prefer composition over inheritance?).

Hope that helps,

很酷不放纵 2024-12-31 17:00:08

也许你应该看看这个:

扩展 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 );
       }

}

这完全取决于你想在控制器中做什么。

Maybe you should take a look at this:

Extending Zend_Controller_Action

I personally extend my Controllers to add some custom functionality to my Controllers.
Example: If check if the request is from a cron job or normal request. Sometimes I check for AJAX requests or disable debugging and profiling.

This is my init function:

       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 );
       }

}

It's really up to you what you want to do in your controllers.

绝不服输 2024-12-31 17:00:08

您还可以使用 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");
    }
}

You can also use a BaseController to check for authorization using the preDispatch method, that is automatically called before the actual action:

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");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文