【发布时间】:2009-09-21 17:53:34
【问题描述】:
我正在查看 CodeIgniter 站点上的演示视频并浏览文档,但我不清楚如何实现 根据用户输入从一个页面动态导航到另一个页面。
例如,我想要一个登录表单,该表单将转发到“成功页面”或“登录失败页面”。
我应该把这个功能放在哪里?
【问题讨论】:
标签: php codeigniter
我正在查看 CodeIgniter 站点上的演示视频并浏览文档,但我不清楚如何实现 根据用户输入从一个页面动态导航到另一个页面。
例如,我想要一个登录表单,该表单将转发到“成功页面”或“登录失败页面”。
我应该把这个功能放在哪里?
【问题讨论】:
标签: php codeigniter
好的,因此对于此登录示例,您将需要 Form Helper、Form Validation Class 和 URL Helper。
class Login extends MY_controller {
function index()
{
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');
// some simple rules
$this->form_validation->set_rules('username', 'Username', 'required|alpha_dash|max_length[30]');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == FALSE) {
// This will be the default when the hit this controller/action, or if they submit the form and it does not validate against the rules set above.
// Build your form here
// Send it to a login view or something
} else {
// The form has been submitted, and validated
// At this point you authenticate the user!!
if ($userIsAuthenticated) {
redirect('controller/action'); //anywhere you want them to go!
} else {
// not authenticated...in this case show the login view with "bad username/password" message
}
}
}
使用 URL Helper 中的 redirect() 函数根据控制器中的逻辑将用户发送到其他页面。
【讨论】:
通常在诸如 Codeigniter 之类的 MVC 框架中,执行动态导航的逻辑将驻留在控制器中。
示例:(在 php 风格的伪代码中)
<?php
class Blog extends Controller {
function login($username, $password)
{
if ($username and $password are correct) {
$this->load->view('success');
return;
}
$this->load->view('fail', $data);
}
}
?>
我实际上并没有使用 Codeigniter 或 PHP,但我有其他语言的 MVC 经验。另外由于我对语言/框架缺乏经验,请不要使用上面的代码......这只是一个例子。 :D
【讨论】: