Call modules actions from another module

Hi all. I’ve created admin module for my app, one of it’s methods calls methods from another modules:




        foreach($this->modules as $moduleRecord) {

                $module = Yii::app()->getModule($moduleRecord->name);

                $controller = Yii::app()->createController($module->id.'/'.'admin');

                $result = $controller[0]->actionMenu();

        }



In modules actions declared in actions() method of controller, controllers which I call in modules have the same name, and action which I call have the same name too. But in result I get the output the same for each module - from first module. I’ve changed the names of actions in modules, and now it works fine, but I need the same names for my actions - to provide some kind of common interface for modules interoperation.

Advice please.

I’m having similar problem when using Yii::app()->runController(‘module/controller/action’) to call a number of module controller/actions in a loop. The modules have common controller/action names. The purpose is to get portlets on a page to be rendered by the respective modules. It seems that runController() will always call the the controller/actions for the first module in the loop eventhough the route specified are for subsequent modules. Below is my sample code:




foreach($modules as $m) {

  ...

  $this->beginClip('content');

  Yii::app()->runController("$m/controller/action");

  $this->endClip('content');

  echo $this->clips['content'];

  ...

}



What am I doing wrong, or is there a better way to do it?

Thanks.

Did you guys find a solution to your problem?

if so: can you please post it here?

That approach is completely wrong, don’d do like that.

Never use Yii::app()->createController($module->id.’/’.‘admin’), it is not necessary.

If you need to share some code, share in a model if it has a meaning, otherways in a compoment.

Create a component (a class in component) and copy here the code you want to share as a static method, then call MYcompoment::myfunction() in both places.

Business logic should NOT reside in controllers, therefore there is no need to call a function of some controller in some other controller.

Take a look at this.

Yes, But the above code and logic can be used in test.


class MyModuleTest extends CDbTestCase {

    public function testUserCreate() {

        $myModule = Yii::app()->getModule('myModule');

        ob_start();

        Yii::app()->runController($myModule->id . '/default/index');

        $content = ob_get_clean();

        $matcher = array('id' => 'someElement');

        $this->assertTag($matcher, $content, 'someElement not found');

    }

}



Interesting. Did you take a look to functional test?

With selenium you can test how it looks on all browser, really cool tool.




foreach($modules as $m) {

  ...

  $this->beginClip('content');

  Yii::app()->runController("$m/controller/action");

  $this->endClip('content');

  echo $this->clips['content'];

  ...

}