2017-12-01 9 views

답변

1

Codeigniter에는 동일한 솔루션이 있습니다. 코드 훅은 후크라고합니다. 사전 컨트롤러/사후 컨트롤러와 같은 특정 이벤트에서 트리거되는 이벤트 처리기와 같습니다. 그러한 문서를 참조 할 수 있습니다.

하지만 당신이해야 할 일은 설정 파일에서 후크를 활성화하는 것입니다. 당신이 뭔가를 컨트롤러 메소드의 실행 전에 실행하려는 때문에

$config['enable_hooks'] = TRUE; 

, 당신은 pre_controller가 또는 post_controller_constructor하는 후크 할 수 있습니다.

당신이 참조하는 ofor 코드의 샘플 조각 :

$hook['pre_controller'] = array(
     'class' => 'Security', 
     'function' => 'checkForSecurity', 
     'filename' => 'Security.php', 
     'filepath' => 'hooks' 
); 

여기에서해야 할 일은 - 지금 당신은 파일을 작성해야합니다 - Security.php을 폴더 (후크)에서. 이 경우, 메소드 checkForSecurity로 보안 클래스를 정의하십시오.

여기에서 할 수있는 일은 - 사용자가 더 이상지나 가기 전에 승인을 받아야합니다. 제한하려는 특정 컨트롤러 영역에 액세스 할 수있는 권한이없는 사용자가있는 경우 사용자를 로그인 페이지로 리디렉션하거나 사용자에게 페이지를 던져서 오류 메시지를 보낼 수 있습니다.

해피 코딩 :)

+0

나는 이것을 점검 할 것이다. 귀하의 답변 주셔서 감사합니다! –

0

당신은 일반적인 컨트롤러 "MY_Controller.php"를 작성하고 모든 컨트롤러를 확장 할 수 있습니다.

  • 설정 파일의 변수는 $ config [ 'subclass_prefix'] = 'MY_'입니다. config 파일에서

  • 당신은


function __autoload($class) 
{ 
    if(strpos($class, 'CI_') !== 0 && file_exists(APPPATH . 'core/'. $class . EXT)) { 
     include_once(APPPATH . 'core/'. $class . EXT); 
    } 
} 
  • 응용 프로그램/핵심 폴더에 MY_Controller.php 만들기의 핵심 폴더 내부의 모든 클래스 파일을로드하는 코드를 다음을 추가 할 필요가

MY_Controller.php에 다음 코드를 입력하십시오.

class MY_Controller 
{ 
    function __construct() { 

     $this->load->library('auth'); 

     $login_check_uris = array(
      'users/profile' // users -> controller name ; profile -> function name 
     ); 
     // check against logged in 
     if (in_array(uri_string(), $login_check_uris)) { 

      if ($this->auth->logged_in() == FALSE) { 

       // check for ajax request 
       if ($this->input->is_ajax_request()) { 

        $return = array('status' => 0, 'msg' => 'Please login to perform this request.'); 
        echo json_encode($return); 
        exit; 

       } else { 

        redirect('users/login'); // login url 
       } 

      } 

     } else if(uri_string() == 'users/login') { 
      // check if already login 
      if ($this->auth->logged_in()) { 
       redirect('users/profile'); // user profile page 
      } 
     } 
    } 
} 
  • $ login_check_uris 안에는 로그인 할 때 확인해야하는 모든 URL을 넣어야합니다.
  • 그런 다음 모든 컨트롤러에서 MY_Controller를 확장하십시오.
+0

나는 이것을 시도 할 것이다, 고마워! –