Zend FlashMessenger 问题
我是 Zend 框架的新手,我有一个问题。 我创建了一个控制器抽象类,它实现了以下功能:
protected function AddError($message) {
$flashMessenger = $this->_helper->FlashMessenger;
$flashMessenger->setNamespace('Errors');
$flashMessenger->addMessage($message);
$this->view->Errors = $flashMessenger->getMessages();
}
protected function activateErrors()
{
$flashMessenger = $this->_helper->FlashMessenger;
$flashMessenger->setNamespace('Errors');
$this->view->Errors = $flashMessenger->getMessages();
}
因此对于每个控制器我都可以使用 $this->AddError($error); 然后我在布局中渲染 $error 。 所以我不想在每个控制器中处理 flashMesenger。
但我必须在执行每个操作时执行 activateErrors 。
例如
,我有一个控制器测试
类 TestController extends MyController {
public function indexAction() {
$this->AddError("Error 1");
$this->AddError("Error 2");
$this->activateErrors();
}
public function index1Action() {
$this->AddError("Esdsd 1");
$this->AddError("sddsd 2");
$this->activateErrors();
}
}
有没有一种方法可以让我在操作结束时在每个控制器的每个操作中执行这个 activateErrors ,而无需重复代码。
我的意思是我不想在每个操作中都包含此代码。也许有一种方法可以将它包含在我的抽象类 MyController 中。
有人有什么想法吗?
谢谢
I am new to Zend framework and I have a problem.
I created a controller abstract class which implements the functions like:
protected function AddError($message) {
$flashMessenger = $this->_helper->FlashMessenger;
$flashMessenger->setNamespace('Errors');
$flashMessenger->addMessage($message);
$this->view->Errors = $flashMessenger->getMessages();
}
protected function activateErrors()
{
$flashMessenger = $this->_helper->FlashMessenger;
$flashMessenger->setNamespace('Errors');
$this->view->Errors = $flashMessenger->getMessages();
}
So for each controller I am able to use
$this->AddError($error);
And then I render $error in layout.
So I want not to deal with flashMesenger in every controller.
but I have to execute the activateErrors when each action is executed.
for example
I have an controller test
class TestController extends MyController {
public function indexAction() {
$this->AddError("Error 1");
$this->AddError("Error 2");
$this->activateErrors();
}
public function index1Action() {
$this->AddError("Esdsd 1");
$this->AddError("sddsd 2");
$this->activateErrors();
}
}
Is there a way that I could execute this activateErrors in each action for every controller at the end of action without duplicating the code.
I mean I do not want to include this code at every action. Maybe there is a way to include it in my abstract class MyController.
Anybody any Idea?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 postDispatch 钩子,在你的父
MyController
中?引用该页面:
也许这可以解决问题?
What about using a postDispatch hook, in your parent
MyController
?Quoting that page :
Maybe this might do the trick ?
实际上,我贡献了对 FlashMessenger 的增强,它提供了许多您需要的功能寻找。
I actually contributed an enhancement to FlashMessenger which provides a lot of the functionality you're looking for.