2012-05-20 2 views
2

codeigniter에서 사용자 인증을 위해 Tank_auth을 사용하고 있습니다. 로그인 한 사용자가 있는지 여부를 확인하려면 사용자 이름과 ID를 얻으려는 경우 컨트롤러의 모든 기능에 다음 코드를 사용해야하므로 매우 짜증이납니다. 내가 다음 있지만 작동하지 않는 것처럼 function __construct() 내부에 다음 코드를 삽입하여 인생을 더 쉽게 만들 수 있다면Codeigniter의 함수 내 변수 __construct()

// If any user is logged in get his/her userid or name 
    if ($this->tank_auth->is_logged_in()) { 

     $data['user_id'] = $this->tank_auth->get_user_id(); 
     $data['username'] = $this->tank_auth->get_username(); 
    } 

그래서 궁금했다.

작동 방법을 알려주십시오. 사전에

감사합니다 :) 당신 만이 컨트롤러 내에서이 배열을 사용하려는 경우

 function __construct() 
    { 
     parent::__construct(); 

     $this->load->helper('url'); 
     $this->load->library('tank_auth'); 


      if ($this->tank_auth->is_logged_in()) { 

     $data['user_id'] = $this->tank_auth->get_user_id(); 
     $data['username'] = $this->tank_auth->get_username(); 
    } 
    } 

답변

4

, 당신은 다음과 같이 사용할 수 있습니다 :

//outside the functions define the $data as controller class property: 

private $data = array(); 

function __construct() 
{ 
    parent::__construct(); 

    $this->load->helper('url'); 
    $this->load->library('tank_auth'); 


    if ($this->tank_auth->is_logged_in()) { 
     $this->data['user_id'] = $this->tank_auth->get_user_id(); 
     $this->data['username'] = $this->tank_auth->get_username(); 
    } 
} 

//and then use it like 

function index(){ 
    $this->load->view("something.php",$this->data); 
} 
+0

또는 같은 $ 데이터 VAR를 정의 할 수 있습니다 보호 .. – zaherg

+0

고마워, 그 작업 :) –

+0

당신은 오신 것을 환영합니다. –