CodeIgniter HMVC extends MX_Controller, unable to use get_instance correctly

I have a controller in / application / core

/application/core/CMS_Controller.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); require APPPATH."third_party/MX/Controller.php"; class CMS_Controller extends MX_Controller { public function __construct() { parent::__construct(); } public function show_something() { echo "something shown"; } } 

I have one more controller in the module (/modules/my_module/controllers/controller.php) which is extended with CMS_Controller

/modules/my_module/controllers/controller.php

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Controller extends CMS_Controller { public function index() { $this->load->view('view'); } } 

And, in view.php (/modules/my_module/views/view.php), I do this: /modules/my_module/views/view.php

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); $ci =& get_instance(); echo $ci->show_something(); ?> 

And I get this error:

Fatal error: calling the undefined CI :: show_something () method in / home / gofrendi / public _html / No-CMS / modules / my_module / views / view.php on line 3

It will work if I do not use MX_Controller and use CI_Controller instead: /application/core/CMS_Controller.php

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); //require APPPATH."third_party/MX/Controller.php"; class CMS_Controller extends CI_Controller { public function __construct() { parent::__construct(); } public function show_something() { echo "something shown"; } } 

Does anyone know what's wrong here?

+7
source share
3 answers

In the application / third _party / MX / Controller.php at the end of the constructor (after line 54) I added

 /* allow CI_Controller to reference MX_Controller */ CI::$APP->controller = $this; 

if you look at the code $, this refers to the current class, which is MX_Controller, and CI :: $ APP refers to CI_controller (see MX / Base.php file)

so now its just ... we will do a link to the CI_Controller (as usual)

  $this->CI =& get_instance(); 

and to get a link to MX_Controller, we will do

  $this->CI =& get_instance()->controller; 
+3
source

I had the same problem, I found that the message and it made my site work, try, maybe?

"You do not need to extend MX_Controller unless you plan to run the controller on another controller. In many cases, the code must be placed in the library. Otherwise, your controller should simply extend MY_Controller."

Found here: http://ellislab.com/forums/viewthread/179478/

+1
source

for me you don't need to get an instance, so my attempt will be like this:

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); echo $this->show_something(); ?> 

instead

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); $ci =& get_instance(); echo $ci->show_something(); ?> 

In any case, itโ€™s good practice to install your own libraries and do somenthing like:

 $this->load->library('foo_lib'); $this->foo_lib->show_somenthing(); 
0
source

All Articles