如何扩展 Zend_Controller_Action 以使函数在所有控制器中通用

发布于 2024-09-18 16:20:00 字数 1455 浏览 6 评论 0原文

我想扩展 Zend_Controller_Action,这样我就可以让消息传递变得通用。现在在 preDispatch() 中我正在设置所有错误和警告消息。如何使 AddMessage(参见代码)和 preDispatch 函数在所有控制器中通用?

<?php

类 PlaygroundController 扩展 Zend_Controller_Action {

public function init()
{
    /* Initialize action controller here */
}

public function preDispatch()
{
    $flashMessenger = $this->_helper->FlashMessenger;

    $flashMessenger->setNamespace('Errors');
    $this->view->Errors = $flashMessenger->getMessages();

    $flashMessenger->setNamespace('Warnings');
    $this->view->Warnings = $flashMessenger->getMessages();

    $flashMessenger->setNamespace('Messages');
    $this->view->Messages = $flashMessenger->getMessages();

    $flashMessenger->setNamespace('Success');
    $this->view->Success = $flashMessenger->getMessages();

}

protected function AddMessage($message,$type='Errors') {
    $flashMessenger = $this->_helper->FlashMessenger;
    $flashMessenger->setNamespace($type);
    $flashMessenger->addMessage($message);
}

public function flashAction()
{
    $this->AddMessage('This is an error message');
    $this->AddMessage('This is another error message');
    $this->AddMessage('This is a warning message','Warnings');
    $this->AddMessage('This is message','Messages');
    $this->AddMessage('This is another success message','Success');
}

}

I want to extend Zend_Controller_Action so I can have messaging be universal. Right now in the preDispatch() I am setting all the error and warning messages. How can I make the AddMessage (see code) and preDispatch functions be universal throughout all controllers?

<?php

class PlaygroundController extends Zend_Controller_Action
{

public function init()
{
    /* Initialize action controller here */
}

public function preDispatch()
{
    $flashMessenger = $this->_helper->FlashMessenger;

    $flashMessenger->setNamespace('Errors');
    $this->view->Errors = $flashMessenger->getMessages();

    $flashMessenger->setNamespace('Warnings');
    $this->view->Warnings = $flashMessenger->getMessages();

    $flashMessenger->setNamespace('Messages');
    $this->view->Messages = $flashMessenger->getMessages();

    $flashMessenger->setNamespace('Success');
    $this->view->Success = $flashMessenger->getMessages();

}

protected function AddMessage($message,$type='Errors') {
    $flashMessenger = $this->_helper->FlashMessenger;
    $flashMessenger->setNamespace($type);
    $flashMessenger->addMessage($message);
}

public function flashAction()
{
    $this->AddMessage('This is an error message');
    $this->AddMessage('This is another error message');
    $this->AddMessage('This is a warning message','Warnings');
    $this->AddMessage('This is message','Messages');
    $this->AddMessage('This is another success message','Success');
}

}

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

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

发布评论

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

评论(4

灼痛 2024-09-25 16:20:00

我确实没有任何 ZF 经验,但我无法想象为什么以下内容不起作用:

abstract class Zend_Controller_MyAction extends Zend_Controller_Action
{
  public function preDispatch()
  {
  ...
  }

  protected function addMessage ($message, $type='Errors')
  {
    ....
  }
  //... other common methods
}

那么你的 PlaygroundController 将扩展 Zend_Controller_MyAction 而不是 Zend_Controller_Action 。

I don't really have any ZF experience, but I can't imagine why the following wouldn't work:

abstract class Zend_Controller_MyAction extends Zend_Controller_Action
{
  public function preDispatch()
  {
  ...
  }

  protected function addMessage ($message, $type='Errors')
  {
    ....
  }
  //... other common methods
}

Then your PlaygroundController will extend Zend_Controller_MyAction instead of Zend_Controller_Action.

习ぎ惯性依靠 2024-09-25 16:20:00

您可以将这两个移动到模型中,并使用 Zend_Controller_Front 插件注入 PreDispatch 事件。

You could move those two into a model, and inject the PreDispatch event using a Zend_Controller_Front plugin.

碍人泪离人颜 2024-09-25 16:20:00

请参阅我的相关问题: Custom Zend_Action_Controller 的正确位置

我刚刚最终将其放在 library/APPNAMESAPCE/Action/Contoller 下,并将其放入 application.ini 中:

autoloadernamespaces.APPNAMESPACE = "APPNAMESPACE_"
appnamespace = "APPNAMESPACE_"

显然,APPNAMESPACE 应该替换为我的命名空间您的申请。您应该已经有第二行来自动加载模型/表单/等。

我还将 library/APPNAMESPACE/ 用于标准 Zend Framework 类的任何其他应用程序特定子类。

更新:已经建议使用插头,对于这个用例来说,这似乎是一个好主意。猜猜我把应用程序特定的插件放在哪里?

library/APPNAMESPACE/Action/Contoller/Plugin

当然,如果这是一个您将用于多个应用程序的插件,那么将其放入一个公共库中是有意义的。

See my related question: Correct Location for Custom Zend_Action_Controller

I've just ended up putting it under library/APPNAMESAPCE/Action/Contoller and throw this in application.ini:

autoloadernamespaces.APPNAMESPACE = "APPNAMESPACE_"
appnamespace = "APPNAMESPACE_"

Obviously, APPNAMESPACE should be replaced my the namespace of your application. You should already have the second line to autoload the models/forms/etc.

I also use the library/APPNAMESPACE/ for any other application specific subclasses of standard Zend Framework classes.

Update: Using a plug is has been suggested, and certainly seems like a good idea for this use case. Guess where I keep my application specific plugins?

library/APPNAMESPACE/Action/Contoller/Plugin

Of course if this is a plugin you'll be using for more than one application, it would make sense to put that in a common library.

娇纵 2024-09-25 16:20:00

您必须子类化 Zend_Controller_Action

//library/My/Controller/Action.php
class My_Controller_Action extends Zend_Controller_Action
{
...
}

然后

class IndexController extends My_Controller_Action
{
...
}

或者,您可以将 Zend Framework 放在项目外部的某个位置,然后重载 Zend_Controller_Action 类,将其放入 /library dir,它将在 Zend 库之前读取。

回答了你的问题后,我的建议是,你应该避免这样做。

在这种情况下,最好的解决方案似乎是编写自定义 flashMessenger。例如,看一下这个:

Zend Framework:查看助手优先级 Messenger |发射

You have to subclass Zend_Controller_Action:

//library/My/Controller/Action.php
class My_Controller_Action extends Zend_Controller_Action
{
...
}

then

class IndexController extends My_Controller_Action
{
...
}

Alternatively, you may put Zend Framework in somewhere outside the project, and then overload Zend_Controller_Action class putting it in /library dir, which will be read before the Zend library.

Having answered your question, my advice is, you should avoid doing this.

In this case the best solution seems to be writing custom flashMessenger. E.g. Take a look at this one:

Zend Framework: View Helper Priority Messenger | emanaton

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