0

I’m learning CI coming from a non MVC OOP in-house framework, so I’m still confused on a bunch of things, I don’t always understand what is normal OO programming VS. things that done the code igniter way. Anyway I’ll try to explain my problem:

I have one controller, called Admin which extends CI_Controller.

I made a class (let’s say custom class meaning is something I made not part od CI) I put that class in the library folder, that class extends CI_Controller as well (that extension allowed me to have load method and some other CI methods).

From my Admin controller, I’m loading my custom class (named “ComponiPannello” since I’m italian ), this way:

$parameters = array('baseUrl' => $this->baseUrl);
$this->load->library('ComponiPannello', $parameters); 

everything works perfectly my “ComponiPannello” does hits job and returns something I need for my Admin view.

Now I wanted to load a model right after that class, because I need to pass more info to the Admin view, those info are stored in the DB, I have a specific model to pull those extra info, so I added a few lines and the code looks as follow:

$parameters = array('baseUrl' => $this->baseUrl);
$this->load->library('ComponiPannello', $parameters);
$setup = $this->componipannello->return_data();
$this->data = array_merge($this->data, $setup);

$this->load->model('categories_model');
$this->data['categories'] = $this->categories_model->get_categories(); 

were $this->data is what I’m passing to the Admin’s view.

Few more info:

  • as I said both Admin controller and my custom class ComponiPannello extends CI_Controller
  • in ComponiPannello I have this constructor:

    function _construct($params) { parent::_construct(); $this->load->library('session'); $this->baseUrl = $params['baseUrl']; }

  • if I move the $this->load->model part before the $this->load->library I get no errors everything works and returns the correct data, but I would like to learn what I’m doing.

  • if I keep it as I have it the error I get is the following:

Fatal error: Call to a member function get_categories() on a non-object in ... on line ...

I hope I was able to explain my issue in a way that is clear enough for you to help me, thanks in advance for the time spent.

Luke

2 Answers 2

4

Your problem is probably coming about because you are loading another controller as a library, when in fact, it is not a library.

You can load libraries, helpers, etc from a library like this:

$CI =& get_instance();

From within your library, instead of $this->load->helper() to load a helper you would now use $CI->load->helper().

More info on this is available in the CI documentation: http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html

Sign up to request clarification or add additional context in comments.

2 Comments

is this: $CI =& get_instance(); going to be placed within the __construct of my loaded library? So from my controller I load the library normally, within the library I put $CI =& get_instance(); i.e. in the construct, then $CI->load->helper() anywhere else?
You can add it to the constructor but unless you make it a class variable you won't have scope from other class functions. Alternatively, just add it to whatever function you need to access it from.
3

Whay u use database object in ur library?

Utilizing CodeIgniter Resources within Your Library. To access CodeIgniter's native resources within your library use the get_instance() function. This function returns the CodeIgniter super object.

Normally from within your controller functions you will call any of the available CodeIgniter functions using the $this construct:

$this->load->helper('url');
$this->load->library('session');
$this->config->item('base_url');
etc. 

$this, however, only works directly within your controllers, your models, or your views. If you would like to use CodeIgniter's classes from within your own custom classes you can do so as follows:

First, assign the CodeIgniter object to a variable:

$CI =& get_instance();

$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');
etc. 

5 Comments

Thanks for your help, I read something like that in the CI manual but I wasn't sure that was my problem, but foremost not 100% clear on how to use it, so basically in my class (ComponiPannello) instead of using $this->load->model I would have to do what you just said right? so $CI =& get_instance();...etc..right?
an other couple of question: is it correct to extend CI_controller form my custom class? From what you wrote above is useless. why did you write "Whay u use database object in ur library?" what should I do isntead? Pull form the controller then pass the result to the class?
What that class library need to do. Explain
I created that class to split what Admin has to do, my Admin controller does several functionality, for this reason I created ComponiPannello (in english ComposePanel) that takes care of rendering a view (basically helps me organizing Admin better). It needs the DB because it set up data for the Admin view, manipulates those data pulled from the DB and returns them as a simple array to Admin, ready to be passed to the view.
For that u dont need new lib class. Go on core folder and make Admin_Controller and extend MX_ or CI_Controller. Tha in ur _constructor define all what u need. U can force login sistem, and all ur page who extend Admin Controller must login in . Try this this is better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.