是否可以在CodeIgniter中使用全局类变量?

时间:2013-06-04 08:18:10

标签: codeigniter variables methods

我正在CodeIgniter中开发一个具有成员登录系统的应用程序。我有一个模型,可以获取所请求成员的所有信息。

class Member extends CI_Model {

var $info = array();
var $error = NULL;

function __construct(){

    parent::__construct();

}

public function get_info($member_id = ''){

    $this->db->where('member_id', $member_id);
    $this->db->limit(1);
    $query = $this->db->get('members');

    if($query->num_rows() > 0){

        $member = $query->row_array();

        $info = array(
            'id' => $member['member_id'],
            'display_name' => $member['display_name'],
            'email_address' => $member['email_address'],
            'password' => $member['password'],
            'status' => ($member['status'] == 0) ? FALSE : TRUE,
            'activation_code' => $member['activation_code'],
            'location' => $member['location'],
            'date_joined' => date('M jS, Y', $member['date_joined']),
            'gender' => ($member['gender'] == 0) ? 'Male' : 'Female',
            'results_per_page' => $member['results_per_page'],
            'admin_emails' => ($member['admin_emails'] == 0) ? FALSE : TRUE,
            'member_emails' => ($member['member_emails'] == 0) ? FALSE : TRUE
        );

        $this->info = $info;

    } else {

        $this->error = 'The member you requested could not be found in our database.';

    }

}

var $info = array(); var $error = NULL; function __construct(){ parent::__construct(); } public function get_info($member_id = ''){ $this->db->where('member_id', $member_id); $this->db->limit(1); $query = $this->db->get('members'); if($query->num_rows() > 0){ $member = $query->row_array(); $info = array( 'id' => $member['member_id'], 'display_name' => $member['display_name'], 'email_address' => $member['email_address'], 'password' => $member['password'], 'status' => ($member['status'] == 0) ? FALSE : TRUE, 'activation_code' => $member['activation_code'], 'location' => $member['location'], 'date_joined' => date('M jS, Y', $member['date_joined']), 'gender' => ($member['gender'] == 0) ? 'Male' : 'Female', 'results_per_page' => $member['results_per_page'], 'admin_emails' => ($member['admin_emails'] == 0) ? FALSE : TRUE, 'member_emails' => ($member['member_emails'] == 0) ? FALSE : TRUE ); $this->info = $info; } else { $this->error = 'The member you requested could not be found in our database.'; } }

在我的控制器和其他模型的顶部,我使用以下内容获取当前用户的信息,将其传递给所有方法。

function __construct(){

    parent::__construct();
    $this->member->get_info($this->session->userdata('member_id'));
    $this->user = $this->member->info;

}

function index(){

 if($this->user['id'] > 0){

      echo "You are logged in!";

 } else {

      echo "You are NOT logged in!";

 }

有没有办法在全球范围内这样做?在每个控制器的顶部输入构造代码是很烦人的。

2 个答案:

答案 0 :(得分:1)

所以我设法在StackOverflow上找到另一个解决我问题的帖子。

enter link description here

在application / core中,我扩展了现有的Controller和Model类,增加了一些内容。然后我不得不改变我的控制器和模型以适应。

class Home extends MY_Controller {

}

应用/型芯/ MY_Model.php

class MY_Model extends CI_Model {

var $user = array();

function __construct(){

    parent::__construct();

    $this->load->model('member');
    $this->member->get_info($this->session->userdata('member_id'));
    $this->user = $this->member->info;

}

var $user = array(); function __construct(){ parent::__construct(); $this->load->model('member'); $this->member->get_info($this->session->userdata('member_id')); $this->user = $this->member->info; }

应用/型芯/ MY_Controller.php }

class MY_Controller extends CI_Controller {

var $user = array();

function __construct(){

    parent::__construct();

    $this->load->model('member');
    $this->member->get_info($this->session->userdata('member_id'));
    $this->user = $this->member->info;

}

答案 1 :(得分:0)

在构造中,只需尝试访问会话数据

function __construct() {
     if($this->session->userdata('member_id')) {
          echo 'You are logged in';
     } else {
          echo 'You are not logged in';
     }
}

这很简单,而不是获取所有数据并选择“用户ID”,如果我们检查会话数据是否存在,那么用户登录orelse没有人被记录。您可以在每个控制器构造函数中添加它你可以在没有任何数据库帮助的情况下进行检查