3

I am using HMVC in my simple project but I don't know how can I call them inside my controller.

Here's my setup

- modules
  - common
    - controllers
      - header
      - footer
    - views
      - header
      - footer
  - foo
    - controllers
      - foo
    - views
      - foo

My header and footer controller:

class Header extends MX_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {

        $data['title'] = "Welcome to HMVC!";

        $this->load->view('header', $data);

    }

}

class Footer extends MX_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {

        $data['links'] = array('link1', 'link2', 'link3', 'link4', 'link5', 'link6');

        $this->load->view('footer', $data);

    }

}

My header and footer view is simple like this:

<!DOCTYPE html>
<html>
    <head>
        <title><?php echo $title; ?></title>
    </head>
    <body>
        <div class="container"><!-- main wrapper -->

....

    <ul style="list-style: none">
        <?php foreach($links as $link) { ?>
            <li><?php echo $link; ?></li>
        <?php } ?>
        </ul>
        </div><!-- end of main wrapper -->
    </body>
</html>

And in my foo controller I call them like this:

    public function __construct() {
        parent::__construct();
        $this->load->model('M_Foo');
    }

    public function index()
    {
        $data['test'] = $this->M_Foo->sampleQuery();

        Modules::run('common/header', $data);
        Modules::run('common/footer', $data);

        $this->load->view('foo_message', $data);


    }

How can I call them inside my controller? I am really new to HMVC.

Jerielle
  • 7,144
  • 29
  • 98
  • 164

1 Answers1

3

It's very rare I run into an exceptionally high quality question, if I could, I'd upvote your question twice. I don't have any experience with HMVC though, just a shot in the dark, but what happens if you try referencing the method rather than the controller?

Modules::run('common/header/index', $data);
Modules::run('common/footer/index', $data);

If that doesn't work, also give this a shot:

$this->load->module('common');
$this->header->index();
$this->footer->index();
Ultimater
  • 4,647
  • 2
  • 29
  • 43
  • Thanks, the first one works! :-) I wonder why do I need to put the index method? – Jerielle Oct 28 '15 at 05:27
  • In every framework I ever used, the router gets confused from module/controller/action structures versus controller/action structures and thus not sure whether to use the default module or the default action. So I can understand how you'd need to put it if your routing logic is not configured "properly". – Ultimater Oct 28 '15 at 05:34
  • Thanks for the big help :-) – Jerielle Oct 28 '15 at 06:33