php - 我怎样才能扩展 Zend_Controller_Action 使一个函数在所有 Controller 中通用

标签 php model-view-controller zend-framework

我想扩展 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/

相关文章:

json - 在 Zend 框架中为 Android/Iphone 应用程序创建 Json Web 服务时使用什么

php - 我应该将变量放在类还是构造函数中? PHP

php - HTML PHP - 如何检查用户名是否已经存在

java - 如何考虑MVC模式下的 "loader class"?

jquery - 从客户端检测到潜在危险的 Request.Path 值

zend-framework - Zend 框架嵌套复选框

php - 行的 SQL 位置(排名系统)两条记录没有相同的排名

php - Laravel,Faker - 增加生成的日期时间

ruby-on-rails - View 中 .html.erb 中的 else 语句

zend-framework - Zend Module Bootstrap 不加载