Codeigniter:访问自定义库上函数的正确方式



我创建了一个名为Xauth.php的简单库来检查用户是否已经登录:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Xauth
{
  protected $ci;
  public function __construct()
  {
    $this->ci =& get_instance();
  }
  public function is_logged_in()
  {
    if ($this->ci->session->userdata('is_logged_in'))
    {
      return true;
    }
    return false;
  }
}

我把这个库放在我的Admin_Controller中,所以无论用Admin_Coontroller扩展了什么控制器,都将首先检查,如果会话数据为空,它们将重定向到登录页面。这是我的Admin_Controller.php:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin_Controller extends MY_Controller {
  public function __construct()
  {
    $this->load->library('Xauth');
    if ($this->Xauth->is_logged_in() == false) {
      redirect('auth');
    }
  }
}

但我有一个错误,上面写着:

Message: Undefined property: Dashboard::$Xauth

我的错在哪里?

您必须使用小写字母的类:

$this->xauth->is_logged_in()

最新更新