2011-12-14 7 views
0

validate_credentials 및 logout 기능이 인덱스 함수 내에 있어야합니까? 그렇지 않다면 좋은 제안이 무엇인지 잘보아야합니다. 나는 더 나은 실천을 배우려고 노력하고있어. 어떤 아이디어?컨트롤러 기능

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Usermanagement extends CI_Controller { 

public function index() 
{ 
    //Config Defaults Start 
    $msgBoxMsgs = array();//msgType = dl, info, warn, note, msg 
    $cssPageAddons = '';//If you have extra CSS for this view append it here 
    $jsPageAddons = '';//If you have extra JS for this view append it here 
    $metaAddons = '';//Sometimes there is a need for additional Meta Data such in the case of Facebook addon's 
    $siteTitle = '';//alter only if you need something other than the default for this view. 
    //Config Defaults Start 


    //examples of how to use the message box system (css not included). 
    //$msgBoxMsgs[] = array('msgType' => 'dl', 'theMsg' => 'This is a Blank Message Box...'); 

    /**********************************************************Your Coding Logic Here, Start*/ 

    $bodyContent = "login";//which view file 
    $bodyType = "full";//type of template 

    function validate_credentials() 
    { 
     $this->load->model('usersmodel'); 
     $query = $this->usersmodel->validate(); 

     if ($query) 
     { 
      $data = array(
       'username' => $this->input->post('username'), 
       'is_logged_in' => true 
      ); 

      $this->session->set_userdata($data); 
      redirect('cpanel'); 
     } 
     else 
     { 
      $this->index(); 
     } 
    } 

    function logout() 
    { 
     $this->session->sess_destroy(); 
     $this->index(); 
    }     

    /***********************************************************Your Coding Logic Here, End*/ 

    //Double checks if any default variables have been changed, Start. 
    //If msgBoxMsgs array has anything in it, if so displays it in view, else does nothing.  
    if(count($msgBoxMsgs) !== 0) 
    { 
     $msgBoxes = $this->msgboxes->buildMsgBoxesOutput(array('display' => 'show', 'msgs' =>$msgBoxMsgs)); 
    } 
    else 
    { 
     $msgBoxes = array('display' => 'none'); 
    } 

    if($siteTitle == '') 
    { 
     $siteTitle = $this->metatags->SiteTitle(); //reads 
    } 

    //Double checks if any default variables have been changed, End. 

    $this->data['msgBoxes'] = $msgBoxes; 
    $this->data['cssPageAddons'] = $cssPageAddons;//if there is any additional CSS to add from above Variable this will send it to the view. 
    $this->data['jsPageAddons'] = $jsPageAddons;//if there is any addictional JS to add from the above variable this will send it to the view. 
    $this->data['metaAddons'] = $metaAddons;//if there is any addictional meta data to add from the above variable this will send it to the view. 
    $this->data['pageMetaTags'] = $this->metatags->MetaTags();//defaults can be changed via models/metatags.php 
    $this->data['siteTitle'] = $siteTitle;//defaults can be changed via models/metatags.php 
    $this->data['bodyType'] = $bodyType; 
    $this->data['bodyContent'] = $bodyContent; 
    $this->load->view('usermanagement/index', $this->data); 
} 
} 

/* End of file usermanagement.php */ 
/* Location: ./application/controllers/usermanagement.php */ 

편집 : 가 할 심지어 validate_credentials 기능에 추가 할 좋은 생각이 있으신가요?

<?php 

class Usermanagement extends CI_Controller { 

public function index() 
{ 
    //Config Defaults Start 
    $msgBoxMsgs = array();//msgType = dl, info, warn, note, msg 
    $cssPageAddons = '';//If you have extra CSS for this view append it here 
    $jsPageAddons = '';//If you have extra JS for this view append it here 
    $metaAddons = '';//Sometimes there is a need for additional Meta Data such in the case of Facebook addon's 
    $siteTitle = '';//alter only if you need something other than the default for this view. 
    //Config Defaults Start 


    //examples of how to use the message box system (css not included). 
    //$msgBoxMsgs[] = array('msgType' => 'dl', 'theMsg' => 'This is a Blank Message Box...'); 

    /**********************************************************Your Coding Logic Here, Start*/ 

    $bodyContent = "login";//which view file 
    $bodyType = "full";//type of template 

    /***********************************************************Your Coding Logic Here, End*/ 

    //Double checks if any default variables have been changed, Start. 
    //If msgBoxMsgs array has anything in it, if so displays it in view, else does nothing.  
    if(count($msgBoxMsgs) !== 0) 
    { 
     $msgBoxes = $this->msgboxes->buildMsgBoxesOutput(array('display' => 'show', 'msgs' =>$msgBoxMsgs)); 
    } 
    else 
    { 
     $msgBoxes = array('display' => 'none'); 
    } 

    if($siteTitle == '') 
    { 
     $siteTitle = $this->metatags->SiteTitle(); //reads 
    } 

    //Double checks if any default variables have been changed, End. 

    $this->data['msgBoxes'] = $msgBoxes; 
    $this->data['cssPageAddons'] = $cssPageAddons;//if there is any additional CSS to add from above Variable this will send it to the view. 
    $this->data['jsPageAddons'] = $jsPageAddons;//if there is any addictional JS to add from the above variable this will send it to the view. 
    $this->data['metaAddons'] = $metaAddons;//if there is any addictional meta data to add from the above variable this will send it to the view. 
    $this->data['pageMetaTags'] = $this->metatags->MetaTags();//defaults can be changed via models/metatags.php 
    $this->data['siteTitle'] = $siteTitle;//defaults can be changed via models/metatags.php 
    $this->data['bodyType'] = $bodyType; 
    $this->data['bodyContent'] = $bodyContent; 
    $this->load->view('usermanagement/index', $this->data); 
} 

function validate_credentials() 
{ 
    $this->load->model('usersmodel'); 
    $query = $this->usersmodel->validate(); 

    if ($query) 
    { 
     $data = array(
      'username' => $this->input->post('username'), 
      'is_logged_in' => true 
     ); 

     $this->session->set_userdata($data); 
     redirect('cpanel'); 
    } 
    else 
    { 
     $this->index(); 
    } 
} 

function logout() 
{ 
    $this->session->sess_destroy(); 
    $this->index(); 
}  

} 

/* End of file usermanagement.php */ 
/* Location: ./application/controllers/usermanagement.php */ 
+1

if (! define ('BASEPATH')) exit ('직접 스크립트 액세스가 허용되지 않음'); 왜냐하면 클래스 자체는 GET/POST 매개 변수에 의해 시작될 수 없기 때문입니다. – waza123

+0

내가 본 적이있는 모든 웹 사이트는 코드에 포함되어 있습니다. –

답변

3

나는 그것이 가능하다는 것을 몰랐다. 대신에 같은 클래스의 private 메소드를 사용하십시오. 문서의 php5에서 OOP (객체 지향 프로그래밍)에 관해 읽을 수 있습니다.

1

작동하지만 작동 방식은 설정할 수 없습니다.

하지만 단계를 백업하는 이유는 중첩 된 이유입니다. 이유가 없습니다. 수업에서 그들 모두를 별개의 방법으로 만드십시오. 당신은 자신이 필요 이상으로 많은 일을하고 있습니다.

관련 문제