我想扩展 Zend_Controller_Action 这样我就可以让消息传递变得通用。现在在 preDispatch() 中,我正在设置所有错误和警告消息。如何使 AddMessage(参见代码)和 preDispatch 函数在所有 Controller 中通用?
<?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');
}
最佳答案
我真的没有任何 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
。
关于php - 我怎样才能扩展 Zend_Controller_Action 使一个函数在所有 Controller 中通用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3681557/