Bryan CS
Bryan CS

Reputation: 611

Codeigniter HMVC Modular Error

I'm new to CI + MX and I tried the Modules::run(); style but I can't seem to let it work.

Here's my directory structure:

/application
-/modules
--/main
---/controllers
----main.php
---/models
---/views
--/connections
---/controllers
----connections.php
---/models
----/group_model.php
---/views
----connection_view.php

main.php controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends MX_Controller {
function __construct(){
    parent::__construct();  
    $this->load->helper('url');

}
function index(){
    echo modules::run('connections/load_connections');      
}
}
?>

connections.php controller:

<?php
class Connections extends CI_Controller{

function __construct(){
    parent::__construct();  
    $this->load->helper('url');

    $this->load->model('connections/group_model');
}

function load_connections(){
    $user_id = 2;           
    $data['tabs'] = $this->group_model->get_groups($user_id);                                       
    $this->load->view('connection_view', $data);        

}
}
?>

group_model.php model:

class Group_model extends CI_Model{

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

/**
 * Get all groups in db
 **/    
function get_groups($user_id){
    $this->db->select('g.group_name');
    $this->db->from('groups AS g');
    $this->db->join('members AS m', 'g.group_id = m.group_id');
    $this->db->where('m.user_id', $user_id);
    return $this->db->get()->result_array();
}
}

My connection_view.php view contains a div and some php codes to display the $data['tabs'] passed as array in load_connections function.

The problem is, I'm getting an error that says:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Connections::$group_model

Filename: controllers/connections.php

Line Number: 14

and

Fatal error: Call to a member function get_groups() on a non-object in    C:\xampp\htdocs\hmvc\application\modules\connections\controllers\connections.php on line 14

I have clearly followed all the instructions provided on MX wiki at https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home and set up everything as needed. My database.php under /application/config is already configured. routes.php is also configured pointing the default controller to main.php

I wonder what I have missed or have done wrong that I can't get it to work.

Codeigniter version: 2.1.3
MX version: 5.4

Upvotes: 0

Views: 2664

Answers (1)

Ahmed-Anas
Ahmed-Anas

Reputation: 5639

almost missed it, extend connections with MX_Controller since you are calling modules::run(). Whenever you want a controller to be called with modules::run(), you extend it with MX_Controller instead of CI_Controller


Your second error is caused because of your first error.

Your first error is most likely caused because it cant open the group_model.

try this

$this->load->model('group_model');

Upvotes: 1

Related Questions