![]() |
Can't acccess personnal library - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Can't acccess personnal library (/showthread.php?tid=65771) |
Can't acccess personnal library - Theo - 07-22-2016 Hi all , I've just created a library called Cms and i've follow those steps : http://www.codeigniter.com/user_guide/general/creating_libraries.html When i try to access function in my library i obtain error : PHP FATAL ERROR : Call to a member function getTags() on a non-object The controller code is : PHP Code: $this->load->library('Cms'); My class is : PHP Code: <?php defined('BASEPATH') OR exit('No direct script access allowed'); I've checked many times mispelled word, but everything seems to be ok !! I'm lost ! Thank for your help ! RE: Can't acccess personnal library - Theo - 07-22-2016 (07-22-2016, 05:48 AM)dubefx Wrote: $data['cms'] = $this->cms->getTags($pageName); Thank you but it doesn't work ! RE: Can't acccess personnal library - Avenirer - 07-22-2016 You are using CI methods inside the library without instanciating the CI. Take a look here: https://codeigniter.com/user_guide/general/creating_libraries.html#utilizing-codeigniter-resources-within-your-library ...I am reffering to the "$this->db..." part. RE: Can't acccess personnal library - Theo - 07-22-2016 (07-22-2016, 06:07 AM)Avenirer Wrote: You are using CI methods inside the library without instanciating the CI. Take a look here: https://codeigniter.com/user_guide/general/creating_libraries.html#utilizing-codeigniter-resources-within-your-library Thank you very much it works ! Here is my new library : PHP Code: <?php defined('BASEPATH') OR exit('No direct script access allowed'); RE: Can't acccess personnal library - Wouter60 - 07-23-2016 Usually, collecting data from your database is done by a Model, not a Library. In a model, you can use the $this->db object without instantiating the super-object. One more tip about your method. You are looping through a result_array(), but it seems that you expect the result to be 1 record. In that case you can return this record like this: PHP Code: $query = $this->CI->db->get_where('cms', array('page_name' => $pageName), 1); RE: Can't acccess personnal library - Theo - 07-23-2016 (07-23-2016, 01:39 AM)Wouter60 Wrote: Usually, collecting data from your database is done by a Model, not a Library. Hello and thank you wouter for your help. I don't really know how to use the personal librairies; but if i understand, i had to call a specific model in the library as we use to do in controllers ? If i use load->model in the library i have to instanciate CI or not ? It's not really clear for me as i develop for the first time my own library ! Thank you very much ! RE: Can't acccess personnal library - PaulD - 07-23-2016 Hi, In the library you extend CI as you have done. The loader class is in the CI object, so you load the model via your CI instance: PHP Code: $this->CI->load->model('whatever_model'); The model though, as it is loaded into the CI instance, does not need to extend CI, just extends the CI_model as normal. PHP Code: <?php defined('BASEPATH') OR exit('No direct script access allowed'); Hope that helps Paul RE: Can't acccess personnal library - Theo - 07-24-2016 (07-23-2016, 04:33 AM)PaulD Wrote: Hi, Thank you Paul for your help too. I knew i was not using an optimized way to code but it was a first shot . Thank you veryyyy muchhhh !! |