2013-03-11 38 views
1

即使在多次訪問文檔後,我仍然在理解控制器利用率方面存在差距。我有以下幾點:codeigniter:控制器函數來加載視圖和處理提交的數據

class Membership extends CI_Controller 
{ 


    /*Load the login view first assuming user is member*/ 
    public function index() 
    { 
     $this->load->view('login'); 
    } 

    /*if not member, load register view*/ 
    public function register() 
    { 
     $this->load->view('register'); 
    } 

    /*view to recover user credentials*/ 
    public function recover() 
    { 
     $this->load->view('recover'); 
    } 

    public function enroll_user() 
    { 
     /*Retrieve post parameters*/ 
     $fullname = $this->input->post('fullname'); 
     $email = $this->input->post('email'); 
     $mobile = $this->input->post('mobile'); 
     $home = $this->input->post('home'); 
     $username = $this->input->post('username'); 
     $password = $this->input->post('password'); 
     $confirmPassword = $this->input->post('cpassword'); 
    } 

} 

現在例如,當用戶點擊立即註冊主頁上鍊接,寄存器()函數應該加載視圖。但是在完成表格並提交之後,我應該提交哪個函數?這是否意味着我必須爲每個功能創建兩個函數,一個用於加載視圖(register.php),另一個用於處理該函數(enroll_user)的操作?

+0

耶顯然你需要做的像只.......... .. – 2013-03-11 06:36:31

+0

我會將註冊頁面提交給自己。如果成功,請在進入其他功能/頁面之前進行所有驗證。 – Jeemusu 2013-03-11 06:37:03

回答

3

爲此,您可以在短短的一個鏡頭

public function register() 
{ 
    $post = $this->input->post(); 
    if($post){ 
     // do other stuff here 
     // you can load view here too if validation fails 
    }else{ 
     $this->load->view('register'); 
    } 
} 

而且在你的方式使用此

echo form_open(current_url()); 
+0

這是非常好的方法。我更喜歡有一個函數處理這兩個功能。謝謝 – 2013-03-11 06:54:14

+0

不客氣!再看到答案 – 2013-03-11 06:55:21

+0

+1 @raheelshan簡單和簡短的答案。 – 2013-03-11 07:03:15

1

在您的register.php把形式memebership/enroll_user的作用,所以當用戶提交註冊表單動作將所需提交enroll_user功能,很會照顧登記等

您的需要兩個功能,一個用於顯示註冊表單,另一個用於處理提交的數據。你可以使用一個控制器來完成它,但是它的好的做法是在兩個控制器中完成它,因爲它會讓你更容易找出哪個函數在做什麼。

相關問題