zend framework - How to run same lines in all controllers init() function? -


i need same 2 lines in controllers, each controller have own init logic, these 2 lines common of them.

public function init() {     $fm =$this->_helper->gethelper('flashmessenger');     $this->view->messages = $fm->getmessages(); } 

how can avoid repeat code ?

update:

ok, flashmessenger example, let's need write log line in every action except 'someaction' @ 'somecontroller'. new common lines should be.

$this->logger = new zend_log(); $writer = new zend_log_writer_stream(application_path.'/../logs/log.txt'); $this->logger->addwriter($writer); $this->logger->log('some message',zend_log::debug); 

the question is, should place these lines in order avoid repeat them in init() of each controller. these lines should placed @ bootstrap?. if so: how can skip log lines 'someaction'. or should implement 'basecontroller' , make controller extend it. if so: how can autoload it? (fatal error: class 'basecontroller' not found) .

just subclass controller:

class application_controlleraction extends zend_controller_action {     public function init()     {         $fm =$this->_helper->gethelper('flashmessenger');         $this->view->messages = $fm->getmessages();     } }   class indexcontroller extends application_controlleraction { } 

you may achieve same writing controller plugin.

edit:

front controller plugins executed on each request, controllers , have same hook methods:

routestartup(): prior routing request routeshutdown(): after routing request dispatchloopstartup(): prior entering dispatch loop predispatch(): prior dispatching individual action postdispatch(): after dispatching individual action dispatchloopshutdown(): after completing dispatch loop 

i addition, may check controller params execute code on selected requests:

if ('admin' == $this->getrequest()->getmodulename()  && 'update' == $this->getrequest()->getactionname() ) … 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -