load a library in a model in codeigniter

load a library in a model in codeigniter  using -'codeigniter'

Why won't my model load the encryption library?

class User_model extends Model {

  function User_model() {

  function check_login() {
    $email = $this->encrypt->decode($email);

This giving me a PHP error: Call to a member function decode() on a non-object on line X -- where X is the $this->encrypt->decode($email); line?

*Edited to show that the problem was that check_login was called from the constructor*

asked Sep 15, 2015 by Ashley26Eygt
0 votes

6 Answers

0 votes

You don't need to load the library in the MODEL, MODELS are always called from the CONTROLLERS so you just have to load the Libraries in the Controller, and the functions will be available in the models called from him!


answered Sep 15, 2015 by howtobulkupfast533
0 votes

I was calling check_login from within the constructor, and that was causing the problems.

The solution is to call $this->_assign_libraries(); right after loading a library in a constructor.

Thanks to this codeignitor forum thread: http://codeigniter.com/forums/viewthread/145537/

answered Sep 15, 2015 by LorBicheno
0 votes

I have tried many of them, but in the end, what I did is this in a model:

$mylib= new Mylib();

This works for me.

answered Sep 15, 2015 by DennisKawamo
0 votes

Libraries should automatically be assigned to the Model instance so it should work fine.

Remember if you can't access the super-global you can always use $ci =& get_instance() to grab it at no extra cost to your memory.

But still... your code example should work >.<

answered Sep 15, 2015 by DYUAlvinvumh
0 votes

i was also facing issue about facebook api, then I tried required_once the lib file of facebook in model. it worked for me.

require_once "application/libraries/facebook.php"; then make its object if you need.

answered Sep 15, 2015 by BennyAllsopi
0 votes

you might want to change the name of the object for the library you are loading beacause CI also has got the encrypt class

just do


Hope this helps

answered Sep 15, 2015 by AudLavater