0

I'm building an application in Codigniter with HMVC. In it I call a module inside another controller. The problem I've run into is trying to pass/retrieve the data loaded into module. Specifically, I'm loading some javascript files that I would then like to pass to the calling controller.

Here is a simplified code:

public function module()
{
    ...
    $this->data['js'] = $this->js_assets;
    ...
    return $this->load->view('module_view', $this->data, true);
}

public function controller()
{
    ...

    $this->load->module('module/module');
    $this->data['module'] = $this->module->module();

    ...
}

I know that I can retrieve data['js'] in module_view as $js, but I wonder if I can just pass the data directly to the controller.

PeterA
  • 15
  • 4
  • I would use `Modules::run('modulename/controller/function', $data);` to pass data –  Dec 11 '15 at 21:57
  • I did try that, but it works in reverse of what I want. It passes data from the controller to the module. What I'd like to do is get data from the module to the calling controller. – PeterA Dec 11 '15 at 23:36

1 Answers1

0

What I'd like to do is get data from the module to the calling controller

Yes, you can do that with HMVC.

In the calling module you can organise code like this (pseudocode):

class Someclass extends CI_Controller {
    ...
    public function show_page()
    {
        $this->load->module("module");
        $js_files = $this->module->get_js_files();
        $this->load->view("header", array("js_files" => $js_files));
    }
    ...
}

In the callee module you would write something along these lines:

class Module extends CI_Controller {
    ...
    public function get_js_files()
    {
        $scripts = $this->frontend_model->get_scripts();
        return $scripts;
    }
    ....
}

(Although in this fantasy case it would be wiser to get data from model in the first place)


Another technique is, like @wolfgang1983 correctly mentioned, to call another module like this:

Modules::run('modulename/controller/function', $data);

and in order to get data from that controller you just have to assign returned data to a variable:

$data_received = Modules::run('modulename/controller/function', $data);

But in this case you can't get variables, only buffered output (like loaded and processed view files or echoed statements).

Vaviloff
  • 16,282
  • 6
  • 48
  • 56
  • This works. If I was getting scripts from the model then you're right it would of been simpler to get them that way directly. In my case the scripts are loaded in constructor as array of uri's that I want to pass to calling module. – PeterA Dec 18 '15 at 22:38