开发者

How can I extend Zend_Controller_Action to make a function universal in all controllers

开发者 https://www.devze.com 2023-01-15 07:33 出处:网络
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) a

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='Error开发者_Python百科s') {
    $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:

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.


You could move those two into a model, and inject the PreDispatch event using a Zend_Controller_Front 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 in application.ini:

autoloadernamespaces.APPNAMESPACE = "APPNAMESPACE_"
appnamespace = "APPNAMESPACE_"

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.


You have to subclass 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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消