[英]How can I extend Zend_Controller_Action to make a function universal in all controllers
I want to extend Zend_Controller_Action so I can have messaging be universal. 我想扩展Zend_Controller_Action,这样我就可以使消息传递成为通用的。 Right now in the preDispatch() I am setting all the error and warning messages.
现在在preDispatch()中,我正在设置所有错误和警告消息。 How can I make the AddMessage (see code) and preDispatch functions be universal throughout all controllers?
如何使AddMessage(请参阅代码)和preDispatch函数在所有控制器中都是通用的?
<?php
class PlaygroundController extends Zend_Controller_Action { class 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 don't really have any ZF experience, but I can't imagine why the following wouldn't work: 我没有任何ZF经验,但我无法想象以下为什么不起作用:
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
. 然后你的
PlaygroundController
将扩展Zend_Controller_MyAction
而不是Zend_Controller_Action
。
您可以将这两个移动到模型中,并使用Zend_Controller_Front插件注入PreDispatch事件。
See my related question: Correct Location for Custom Zend_Action_Controller 请参阅我的相关问题: 自定义Zend_Action_Controller的正确位置
I've just ended up putting it under library/APPNAMESAPCE/Action/Contoller
and throw this in application.ini
: 我刚刚把它放在
library/APPNAMESAPCE/Action/Contoller
下并将它放在application.ini
:
autoloadernamespaces.APPNAMESPACE = "APPNAMESPACE_"
appnamespace = "APPNAMESPACE_"
Obviously, APPNAMESPACE
should be replaced my the namespace of your application. 显然,
APPNAMESPACE
应该替换你的应用程序的命名空间。 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. 我还将
library/APPNAMESPACE/
用于标准Zend Framework类的任何其他特定于应用程序的子类。
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. 当然,如果这是一个插件,你将用于多个应用程序,将它放在一个公共库中是有意义的。
You have to subclass Zend_Controller_Action
: 你必须
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. 或者,您可以将Zend Framework放在项目之外的某个位置,然后重载
Zend_Controller_Action
类,将它放在/library
目录中,它将在Zend库之前读取。
Having answered your question, my advice is, you should avoid doing this. 回答了你的问题,我的建议是,你应该避免这样做。
In this case the best solution seems to be writing custom flashMessenger
. 在这种情况下,最好的解决方案似乎是编写自定义
flashMessenger
。 Eg Take a look at this one: 例如,看一下这个:
Zend Framework: View Helper Priority Messenger | Zend Framework:查看Helper Priority Messenger | emanaton
emanaton
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.