Codeigniter – accessing CI Library from any files

컨트롤러는 CI_Controller 를 상속받으므로 어느 library 든지 접근이 가능하다.

보통 $this->sesseion->func(), $this->input->func() 이런식으로 사용하게 된다.

그러나 config 폴더안의 파일 등 CI_Controller 를 상속받지 않는 파일안에서는 $this 를 통한 CI 라이브러리 접근이 불가하다.

이럴 때는 아래와 같이 get_instance() 를 호출하여 CI 컨트롤러를 참조시키면 된다.

 

$CI =& get_instance();
if( $CI->session->userdata(‘user_authen’) ){
redirect(base_url().’admin/login/’);
}