Dr Casper Black
Dr Casper Black

Reputation: 7478

cross module communication zend framework php

How can i call a controller::action from another modules controller::action in Zend.

dir tree

-modules
--auth
---controllers
--crm
--default
---controllers

how can i do something like this:

/* module\default\controller */

public function indexAction(){
        $something = \model\auth\IndexController::doSomething();
}

UPDATE:

I know that something like this is possible in CodeIgniter via Modular Extensions see here

$out = modules::run('module/controller/method', $param1, ....);

Upvotes: 1

Views: 1298

Answers (4)

Ademir Mazer Jr - Nuno
Ademir Mazer Jr - Nuno

Reputation: 900

If you need to call an action from another, without go foward with the flow of your program, so there something wrong like RockyFord said.

Thinking in your explanation about a partial retrieving of information to buil a widget, I would create a helper in the library so it could be called from any place, something like:

-lib
--MyLib
---Controller
----Action
-----Helper
------ Foo.php

The Foo action helper could be:

class MyLib_Controller_Action_Helper_Foo extends Zend_Controller_Action_Helper_Abstract {

   public setBar() { 
      // some code here
   }       

   public getBar() { 
      // some code here for retrieving the partial
   }
}

Then to call it from another controller action or even another lib function

 // in a controller action
 ...
 $foo = new MyLib_Controller_Action_Helper_Foo();
 $foo->setBar();
 $bar = $foo->getBar();
 $this->view->bar = $bar;
 ...

Hope this helps

Namastê !!

Upvotes: 1

Francis Yaconiello
Francis Yaconiello

Reputation: 10919

I agree with the RockyFord's suggestion that the action stack helper is probably your best bet.

Other solutions might include manually forwarding to another action ssomewhere else in your app with some paramaters.

function fooAction()
{
    // Going to someplace else
    $this->_forward($action, $controller, $module, $params);
}

Upvotes: 0

ACNB
ACNB

Reputation: 846

If you want to do that, something is probably wrong with your design. Try to move the desired functionality to a third class for example to an action helper and call it from both controllers. That said, it should be possible to do

$a = new A_Controller();
$a->aAction();

or

A_Controller::aAction();

if aAction is declared static. (I have tried neither though.)

Upvotes: 0

RockyFord
RockyFord

Reputation: 8519

The only thing I know of that will approach that functionality is the ActionStack helper:
ZF Action stack helper
Controllers are not really intended to be called in the same manner as most other methods.

Upvotes: 2

Related Questions