首页 文章

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

提问于
浏览
0

我创建一个名为 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_Controller扩展的控制器,如果会话数据为空,它们将重定向到登录页面 . 这是我的 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

我的错在哪里?

1 回答

  • 1

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

    $this->xauth->is_logged_in()
    

相关问题