将函数中的会话变量传递给codeigniter中的帮助器

时间:2022-11-23 20:44:38

Here's what I'm trying to do. This is the function in the controller

这就是我想要做的。这是控制器中的功能

public function get_started()
{
    if(test_login($this->session->all_userdata())) {
        $this->load->view('template');
    } else {
        $this->load->view('error');
    }
}

This is the helper

这是帮手

function test_login($sessdata)
{
    if($sessdata->userdata('is_logged_in')) {
        return true;
    } else {
        return false;
    }
}

I have entered is_logged_in as a boolean session variable. However, this doesn't work.

我已将is_logged_in作为布尔会话变量输入。但是,这不起作用。

I can't find the error.

我找不到错误。

1 个解决方案

#1


18  

instead of passing session data as parameter to your helper, you could access the session from helper itself, like:

而不是将会话数据作为参数传递给助手,您可以从助手本身访问会话,如:

function test_login() {
  $CI = & get_instance();  //get instance, access the CI superobject
  $isLoggedIn = $CI->session->userdata('is_logged_in');
  if( $isLoggedIn ) {
     return TRUE;
  }
  return FALSE;  
}

And controller:

和控制器:

public function get_started(){
   if( test_login() ) {
      $this->load->view('template');
   }
   else {
      $this->load->view('error');
   }
}

#1


18  

instead of passing session data as parameter to your helper, you could access the session from helper itself, like:

而不是将会话数据作为参数传递给助手,您可以从助手本身访问会话,如:

function test_login() {
  $CI = & get_instance();  //get instance, access the CI superobject
  $isLoggedIn = $CI->session->userdata('is_logged_in');
  if( $isLoggedIn ) {
     return TRUE;
  }
  return FALSE;  
}

And controller:

和控制器:

public function get_started(){
   if( test_login() ) {
      $this->load->view('template');
   }
   else {
      $this->load->view('error');
   }
}