Page 1 of 1

Controller calling another controller

Posted: Tue Nov 06, 2012 5:59 pm
by charlesso
Is it possible for controller A to call another controller B, let B prepares the data, and pass control and data back to A?

Re: Controller calling another controller

Posted: Tue Nov 06, 2012 9:11 pm
by sv2109
Try method getChild($route, $args) from /system/engine/controller.php

Re: Controller calling another controller

Posted: Tue Nov 06, 2012 9:18 pm
by charlesso
sv2109 wrote:Try method getChild($route, $args) from /system/engine/controller.php
Great! Thanks sv2109.

Ummm. Now i need to search for documentation n maybe sample code....

Re: Controller calling another controller

Posted: Wed Nov 07, 2012 2:30 am
by sv2109
Try this:

Code: Select all

class ControllerModuleModule1 extends Controller {
  protected function index() {
    $result = $this->getChild('module/module2');
  }
}

Re: Controller calling another controller

Posted: Wed Nov 07, 2012 10:41 am
by Qphoria
Look at the catalog/controller/common/home.php file
You'll note that it has no real content. But it calls its children, which are modules... but modules are just mini-controllers that don't call the output dispatch directly, because the main controllers do it.

But you can pretty much load any module and place it. The module name is its exported variable name "latest" module is called by "$latest", etc

Re: Controller calling another controller

Posted: Wed Nov 07, 2012 3:14 pm
by charlesso
Qphoria wrote:Look at the catalog/controller/common/home.php file
You'll note that it has no real content. But it calls its children, which are modules... but modules are just mini-controllers that don't call the output dispatch directly, because the main controllers do it.

But you can pretty much load any module and place it. The module name is its exported variable name "latest" module is called by "$latest", etc

Thanks Q.

Re: Controller calling another controller

Posted: Thu Jun 27, 2013 4:51 am
by commanddotcom
sv2109, came up with a nice solution.

But can anyone explane why ob_start() ignores?

Code: Select all

ob_start();
   $this->getChild('checkout/cart/add'); // for some reason we have echo here
$result = ob_get_clean(); 
// $result = NULL	

Re: Controller calling another controller

Posted: Sat Jun 29, 2013 2:01 am
by aldoanizio
sv2109 wrote:Try method getChild($route, $args) from /system/engine/controller.php
Hey Man. Don't Take Me Wrong, But...I Love You :laugh:

Saved my life. Thanks for the Tip.

Re: Controller calling another controller

Posted: Fri May 29, 2015 8:49 am
by charlesso
Qphoria wrote:Look at the catalog/controller/common/home.php file
You'll note that it has no real content. But it calls its children, which are modules... but modules are just mini-controllers that don't call the output dispatch directly, because the main controllers do it.

But you can pretty much load any module and place it. The module name is its exported variable name "latest" module is called by "$latest", etc
Hi Q,

This getChild() method can call a child.

But how do I call the Child's methods and pass variables to the methods from the calling controller?

Re: Controller calling another controller

Posted: Sun May 31, 2015 2:30 am
by JNeuhoff
In OpenCart 2.0.x versions, this is quite simple:

Code: Select all

$this->load->controller( $route, $args );
This will execute the action specified by the route, with the optional given args, and returns the rendered output.