im currently working with CodeIgniter.
My Front Controller loads 1 of 2 Library depending on the input he gets.
Both of the librarys need access to the model.
My question:
Should I load the model in each library (plus get_instance() of CI in every Library to even get access to the CI load func) or is there any “better” solution to load the model in the Frontcontroller und ‘pass’ the model to either one or the other library. I even check the autoload possibilty but even then I dont have direct access from within the librarys.
I guess it isnt the best solution to load the model in every library (like, what if i need more libraries and so on) , but im kind of stuck right now.. 🙁
In a nutshell:
– load the model in the FrontController and maybe “pass” the model as an argument to any library I need?
– or get the instance of CI within any library und then load the model (and write redundant code)
– or is there even a way better solution?
thx in advance 🙂
edit:
I go with:
public function __construct()
{
$this->ci_instance =& get_instance();
$this->ci_instance->load->model(‘my_model’);
$this->_model = $this->ci_instance->my_model;
}
and then use the functions like this:
$mysql_result = $this->_model->get_testdata();
It is a little bit shorter than the solution from the tank auth lib (but almost the same). 🙂
But the Tank Auth Lib helped me too (to see how the pros do it).
Thx for that too dm03514!
get_instance is a native way suggested by official docs:
for easier access to model object I would define a property in your library:
somewhere in your library consructor:
then you can access the model in any library method by calling
$this->_model->method();