4
votes

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='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');
}

}

4

4 Answers

2
votes

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.

2
votes

You could move those two into a model, and inject the PreDispatch event using a Zend_Controller_Front plugin.

1
votes

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.

1
votes

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