CodeIgniter Forums

Full Version: ME HMVC + custom library + fetch_module() does not work right
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]wilso417[/eluser]
$CI->router->fetch_module() for HE HMVC does not behave correctly in the following situation.

A custom library I made:

class Test_Library {

function shout_module() {

$CI =& get_instance();
echo $CI->router->fetch_module()."<br />";
}
}

A module/controller I made:

class Test2 extends MX_Controller {

public function index() {

$this->load->library('test_library');
$this->test_library->shout_module();
}
}

A module/controller I run:

class Test1 extends MX_Controller {

public function index() {

$this->load->module('test2');
$this->test2->index();

$this->load->library('test_library');
$this->test_library->shout_module();

}
}

The output is:

test2
test2

The expected output is:

test2
test1

I am using fresh installs and the newest version of CI (2.0.2) & ME HMVC (5.4). I have tried to auto-load the library and remove the load->library calls and that did not work either. I even tried a solution similar to form_validation->CI = &$this. Where a $CI variable inside the library was set before shout_module() was called and that did not work either.

This was just a sample I made to demonstrate the issue. I really need a library to see what module called it for searching/templating purposes. I know I could have that module pass the name of itself, but I was hoping there would be a better work around then that, it's a lot of code I'd have to change.

El Forum

[eluser]wilso417[/eluser]
I actually made the previous post more complicated than it needs to be to illustrate this bug. Here is an even simpler example:

class Test2 extends MX_Controller {

public function index() {
echo $this->router->fetch_module() . "<br />";
}
}

class Test1 extends MX_Controller {

public function index() {

$this->load->module('test2');
$this->test2->index();

echo $this->router->fetch_module() . "<br />";
}
}

El Forum

[eluser]jtrainaldi[/eluser]
I am experiencing the same issue. I am able to use the fetch_module() function but if I load a module inside a module the fetch_module() no longer returns a value.

Did you figure out a solution to this?

El Forum

[eluser]wilso417[/eluser]
I always get it to return a value, just not the value i'm expecting. I do a hack where I turn the module_name back to the parent module name after a function call.

so here is an example where on a page I want a partial view of a side menu that comes from a dashboard module:

function index() {

$this->load->module('dashboard');
$side_nav = $this->dashboard->fetch_side_nav($this->module_name);

echo $this->router->fetch_module();

}

function fetch_client_nav($module_name = NULL) {

// generate side nav

// set the router's module back to the parent module name because it gets changed automatically by HMVC
if (!empty($module_name)) $this->router->module = $module_name;

return $side_nav;
}

El Forum

[eluser]wilso417[/eluser]
oh yeah, $this->module_name exists because I modified MX/Controller.php as well

class MX_Controller {

public $module_name;

public function __construct() {

// set base module variables
$this->module_name = $this->router->fetch_module();
}
}

so this gives me about the most flexibility. if i'm in a controller i can call $this->module_name and always get the current module i'm working in. If a library needs the current module name, like my template library, I call $this->router->fetch_module() and I just need to make sure that this is always set correctly, and to help with this I switch the $this->router->module to the correct module like in my first example.

El Forum

[eluser]jtrainaldi[/eluser]
I came up with a solution and I don't know if it is the best way to go about but it seems to work for me.

In my modules I have the a similar setup through all the controller classes so if I need to create another controller class I can simple copy the file and change the name.


/module/config.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['module_name'] = 'tmp_mod';

---

class Tcontroller extends MX_Controller {

/**
* Name: recordlist function
* Desc: This function loads the database records in a table
*/
public function recordlist(){

//Declare and initialize variables
$data['filter'] = $this->uri->uri_to_assoc(4);
$data['content'] = $this->config->item('module_name') . '/' . $this->router->fetch_class() . '/recordlist';

//Load the view
$this->load->view('templates/fluid', $data);
}


--


This allows me to call any module within a module and I will be guaranteed to always get the correct view files.

If there is a better way to go about this or I am missing something let me know.

P.S. on a side note is there a way for my module controllers to extend a main controller in that module. Basically I would like set up permission checks on each module and I thought having a class Main extends MX_Controller and have all the other classes extend that module.

For example class Tcontroller extends Main { }