2012-11-07 78 views
-1

我有一個使用CI表單驗證的登錄表單。CodeIgniter從URL中刪除功能

形式是在domain.com/login

它運行一個函數調用validate_credentials並且如果驗證失敗它重新加載圖。問題是URL繼續顯示:

domain.com/login/validate_credentials

我如何刪除/ validate_credentials?

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

class Login extends CI_Controller { 

    public function index() 
    { 
    $this->load->library('form_validation'); 

    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email'); 
    $this->form_validation->set_rules('password', 'Password', 'required'); 

    $this->form_validation->set_error_delimiters('<div class="alert alert-error">', '</div><br />'); 

    if ($this->form_validation->run() == FALSE) 
    { 
     $this->load->view('login/login'); 
    } 
    else 
    { 
     $this->load->view('clock/clock'); 
    } 

} 

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

    if($query) {// staff credentials validated... 
     $data = array(
       'email' => $this->input->post('email'), 
       'staff_logged_in' => TRUE 
      ); 

      $this->session->set_userdata($data); 
      redirect('clock/clock'); 
    } 

    else { // not validated reload index function 
     $this->index(); 
    } 
} 

} 

回答

1

而不是調用其他控制器功能,做一個重定向到索引。由於CodeIgniter中的控制器中的函數正在設置URL方案,因此您現在執行的方式只是執行索引函數,但是在validate_credentials函數(和URL)中。

這樣做:

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

class Login extends CI_Controller { 

    public function index() 
    { 
    $this->load->library('form_validation'); 

    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email'); 
    $this->form_validation->set_rules('password', 'Password', 'required'); 

    $this->form_validation->set_error_delimiters('<div class="alert alert-error">', '</div><br />'); 

    if ($this->form_validation->run() == FALSE) 
    { 
     $this->load->view('login/login'); 
    } 
    else 
    { 
     $this->load->view('clock/clock'); 
    } 

} 

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

    if($query) {// staff credentials validated... 
     $data = array(
       'email' => $this->input->post('email'), 
       'staff_logged_in' => TRUE 
      ); 

      $this->session->set_userdata($data); 
      redirect('clock/clock'); 
    } 

    else { // not validated reload index function 
     //$this->index(); 
     $this->load->helper('url'); //to enable redirect function 
     redirect('index'); //based on your index url 
    } 
} 

} 

如果你需要顯示錯誤或成功的消息,請參考笨的session library的flash_message,但這裏有一個例子。

在控制器中設置錯誤信息,中庸之道重定向到索引之前:

$this->load->library('session'); 
$this->session->set_flashdata('error_message', "This is my error message"); 

而在你的索引視圖,你如果它存在它顯示:

<?if($this->session->flashdata('error_message')):?> 
    <div class="nNote nFailure"><p><?=$this->session->flashdata('error_message');?></p></div> 
<?endif;?> 

請注意, flash_message顯示一次。這意味着您的索引將顯示您的錯誤,然後銷燬會話消息。這是flash_message的目的,正確和簡單地顯示錯誤消息。

+0

感謝讓,這是有道理的,但如何將錯誤消息傳回?我看着flashdata,但我不知道如何獲取錯誤。我對CI很陌生。 –

+0

非常好,謝謝! –