简体   繁体   中英

My custom library function is not working

I am gettig a error on my library

Undefined property: Authenticate::$ci

Here is my custom library function

function is_logged_in() {

  $sessionid = $this->ci->session->userdata('moderId');

  if($sessionid) {
    return isset($sessionid);
  } else if(!$sessionid) {
    redirect(base_url() . 'moderator');
  }

}

Here is my controller

class B2bcategory extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('moderator/b2bcategory_model');

        $this->authenticate->is_logged_in();
    }
}

I did not see if you had loaded the get_instance() in your construct area of library, Try some thing like this in your authenticate library

Filename: Authenticate.php

<?php

class Authenticate {

    public function __construct() {
       $this->CI =& get_instance();
    }

    function is_logged_in() {

       $sessionid = $this->CI->session->userdata('moderId');

       if($sessionid) {
          return isset($sessionid);
       } else if(!$sessionid) {
          redirect(base_url() . 'moderator');
       }

    }
}

I have found that seems to work better with $this->CI on your library with get_instance.

To load library on controller if you have not autoload it use.

Filename: B2bcategory.php

<?php

class B2bcategory extends CI_Controller {

   public function __construct() {
    parent::__construct();
     // Or Auto load it
     $this->load->library('authenticate');
     $this->load->model('moderator/b2bcategory_model');
     $this->authenticate->is_logged_in();
   }

}
$this->load->model('moderator/b2bcategory_model', 'authenticate');

$this->load->model('REAL_MODEL_PATH', 'PROPERTY_IN_CONTROLLER')

USAGE $this->PROPERTY_IN_CONTROLLER->library_method()

but best way is in application/core create ModeratorController.php

ModeratorController extends CI_Controller
{
     public function __construct()
     {
          parent::__construct();

          if($this->session->userdata('moder_id') === false)
          {
              redirect('site/moder_login');
          }
     }
}

And all moder controllers extend from this controller

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM