컨트롤러가 _construct
인 경우 컨트롤러에 로그인했는지 여부를 알 수 있습니다. 일치하지 않으면 로그인 화면으로 보내십시오.
function __construct() {
parent::__construct();
if (!$this->session->userdata('logged_in')) {
redirect('YourLoginController');
}
}
이것은 컨트롤러에 있어야합니다.
기본 컨트롤러를 만들어 일반 컨트롤러 CI_Controller
을 확장하고 문서에서 MY_Controller
개념을 찾아 볼 수도 있습니다. 거기에, 당신은 인증을 확인하고하지 않을 경우 리디렉션하는 방법을 추가 할 수 있습니다, 다음 인증이 필요한 컨트롤러의 메소드를 호출하여
class Some_Controller extends MY_Controller {
function _construct() {
parent::_construct();
}
// If a method requires authentication
function someMethod() {
$this->authenticated(); //This does nothing if logged in
//It redirects to login if not logged in
//Your stuff.
}
//If a method DOESN'T require login, your $this->data to
//pass to the view has already been started from MY_Controller
//so append the display content you need to that array and
//then pass it to the view
function someOtherMethod() {
$this->data['somecontent'] = "I'm content";
$this->load->view('someView',$this->data);
}
}
:
class MY_Controller extends Controller{
public $data = array();
function _construct() {
parent::_construct();
$data['logged_in'] = $this->session->userdata('logged_in');
}
function authenticated() {
if (!$this->data['logged_in']) {
redirect('YourLoginController');
}
}
}
다음 컨트롤러를 someOtherMethod()
에서 만든 개념을 사용하면 사용자의 인증 상태에 따라 내용을 변경하기 위해보기에서 변수 $logged_in
을 사용할 수 있습니다.