24
votes

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.

Solution

Better idea is to define a AppController and make all usual controllers to extend AppController which further extends Zend_Controller_Action.

class AppController extends Zend_Controller_Action {
    public function memberFunction() {
         // a resuable function
    }
}

class FirstController extends AppController {
    public function indexAction() {
         // call function from any child class
         $this->memberFunction();
    } 
}

Now memberFunction can be invoked from controllers extending AppController as a rule of simple inheritance.

3
Just an FYI, I definitely prefer to setup shared classes or modules when I need common functionality. However, I ran into one situation using ZF2 where it would be quite helpful to use a parent AppController that is extended on a couple other controllers. Your solution worked nicely. In ZF2 you setup:"class AppController extends AbstractActionController" and the child controller would be the same: "class FirstController extends AppController" I know there are better ways to do this and I should probably rethink some app structure, but it works.gregthegeek

3 Answers

26
votes

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.

5
votes

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...

3
votes

I would suggest you to follow DRY and move those functions to common library place. For example create in library folder

My/Util/ 

and file

CommonFunctions.php

then call your class

My_Util_CommonFunctions

and define your methods

Now you can call them from any place in the code using your new namespace which you have to register.

$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace(array('My_'));

in any controller you can call your custom methods by using:

My_Util_CustomFunctions::yourCustomMethod($params);