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

I want to extend Zend_Controller_Action so that messaging is universal. Right now in preDispatch () I am setting up all error and warning messages. How can I make the AddMessage (see Code) and preDispatch functions universal in all controllers?

<?php 

the PlaygroundController class 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
source share
4 answers

I have no ZF experience, but I cannot imagine why the following did not work:

 abstract class Zend_Controller_MyAction extends Zend_Controller_Action { public function preDispatch() { ... } protected function addMessage ($message, $type='Errors') { .... } //... other common methods } 

Your PlaygroundController will then extend Zend_Controller_MyAction instead of Zend_Controller_Action .

+2
source

You can transfer these two into the model and introduce the PreDispatch event using the Zend_Controller_Front plugin.

+2
source

See my related question: Proper location for custom Zend_Action_Controller

I just put it under library/APPNAMESAPCE/Action/Contoller and threw it into application.ini :

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

Obviously, APPNAMESPACE should be replaced with my application namespace. You should already have a second line for automatic loading of models / forms, etc.

I also use library/APPNAMESPACE/ for any other subclasses of specific applications of the standard Zend Framework classes.

Update. Using a plugin has been suggested and certainly seems like a good idea for this use case. Guess where I keep my plugins for specific applications?

library/APPNAMESPACE/Action/Contoller/Plugin

Of course, if this is a plugin that you will use for several applications, it would be advisable to put it in a shared library.

+1
source

You must 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 can place the Zend Framework somewhere outside the project, and then overload the Zend_Controller_Action class by placing it in the /library directory that will be read before the Zend library.

When answering your question, my advice is that you should avoid this.

In this case, the best solution is to create a custom flashMessenger . For instance. Take a look at this:

Zend Framework: View Priority Assistant | emanaton

+1
source

All Articles