如何扩展 Zend_Controller_Action 以使函数在所有控制器中通用
我想扩展 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我确实没有任何 ZF 经验,但我无法想象为什么以下内容不起作用:
那么你的 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:
Then your
PlaygroundController
will extendZend_Controller_MyAction
instead ofZend_Controller_Action
.您可以将这两个移动到模型中,并使用 Zend_Controller_Front 插件注入 PreDispatch 事件。
You could move those two into a model, and inject the PreDispatch event using a Zend_Controller_Front plugin.
请参阅我的相关问题: Custom Zend_Action_Controller 的正确位置
我刚刚最终将其放在
library/APPNAMESAPCE/Action/Contoller
下,并将其放入application.ini
中:显然,
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 inapplication.ini
: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.
您必须子类化
Zend_Controller_Action
:然后
或者,您可以将 Zend Framework 放在项目外部的某个位置,然后重载
Zend_Controller_Action
类,将其放入/library
dir,它将在 Zend 库之前读取。回答了你的问题后,我的建议是,你应该避免这样做。
在这种情况下,最好的解决方案似乎是编写自定义
flashMessenger
。例如,看一下这个:Zend Framework:查看助手优先级 Messenger |发射
You have to subclass
Zend_Controller_Action
:then
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