2011-12-14 11 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 */ 

EDIT: も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

私はそれが可能であったことさえ知らなかった - あなたは全くそれをするべきではない。代わりに同じクラスのプライベートメソッドを使用してください。ドキュメントのphp5のOOP(オブジェクト指向プログラミング)について読むことができます。

1

これはうまくいくかもしれませんが、設定した方法ではできません。

しかし、あなたはそれらを入れ子にしているステップをバックアップします。理由はありません。クラスの下でそれらをすべて別々のメソッドにするだけです。あなたは自分が必要以上に仕事をしています。

関連する問題