views:

1381

answers:

3

Is it possible to call the member function of another controller in zend framework, if yes then how?

<?php
class FirstController extends Zend_controller_Action {
    public function indexAction() {
         // general action 
    }   

    public function memberFunction() {
         // a resuable function
    }
}

Here's another controller

<?php
class SecondController extends Zend_Controller_Action {
    public indexAction() {
         // here i need to call memberFunction() of FirstController
    }
}

Please explain how i can access memberFunction() from second controller.

+1  A: 

Controllers aren't designed to be used in that way. If you want to execute an action of the other controller after your current controller, use the _forward() method:

// Invokes SecondController::otherActionAction() after the current action has been finished.
$this->_forward('other-action', 'second');

Note that this only works for action methods (“memberAction”), not arbitrary member functions!

If SecondController::memberFunction() does something that is needed across multiple controllers, put that code in a action helper or library class, so that both controllers can access the shared functionality without having to depend on each other.

Ferdinand Beyer
thanks @Ferdinand , it really helped me !!!
Ish Kumar
A: 

see the ActionStack plugin (see the manual, I couldn't post links because I'm a new user :D) it allows you to stack multiple actions

Bye

+1  A: 

You should consider factoring out the code into either an action helper or to your model so that it can be called from both controllers that need it.

Regards,

Rob...

Rob Allen