CI架構的一個問題

來源:互聯網
上載者:User

比如現在有一個 login 方法,是用來輸出 login 視圖

public function login(){    $this->load->view('login');}

此時的登陸頁面是 index/login

然後提交表單資料到 login_check()方法

public function login_check(){    $this->form_validation->set_rules('email', 'email', 'trim|required|valid_email');    $this->form_validation->set_rules('password', 'password', 'trim|required');    if ($this->form_validation->run() === true) {        //.....    }else{        $this->load->view('login');    }}

$this->form_validation->run()表單校正失敗,再次輸出 login 視圖,同時顯示提示錯誤。

那麼此時登陸頁面地址 index/login_check

這個感覺太不好了,不希望連結地址變化。除了將 login()login_check()合并處理可以實現之外,還有什麼更好的辦法嗎?

請多指教,謝謝大家。

回複內容:

比如現在有一個 login 方法,是用來輸出 login 視圖

public function login(){    $this->load->view('login');}

此時的登陸頁面是 index/login

然後提交表單資料到 login_check()方法

public function login_check(){    $this->form_validation->set_rules('email', 'email', 'trim|required|valid_email');    $this->form_validation->set_rules('password', 'password', 'trim|required');    if ($this->form_validation->run() === true) {        //.....    }else{        $this->load->view('login');    }}

$this->form_validation->run()表單校正失敗,再次輸出 login 視圖,同時顯示提示錯誤。

那麼此時登陸頁面地址 index/login_check

這個感覺太不好了,不希望連結地址變化。除了將 login()login_check()合并處理可以實現之外,還有什麼更好的辦法嗎?

請多指教,謝謝大家。

你用ajax提交表單就好了, 這樣不會導致頁面跳轉到login_check

驗證失敗就跳轉到login?error=err_code

CI裡面使用redirect('index/login') 這個函數進行跳轉

在你的else裡使用

想優雅的解決這個問題,需要用到路由,
判斷 http 動詞,映射到相應方法即可

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.