【发布时间】:2026-02-10 06:20:05
【问题描述】:
我有一个相当标准的 CI3 站点正在运行。我创建了自己的基本控制器,称为MY_Controller,我所有的页面控制器都扩展了它。
MY_Controller.php
public function __construct() {
parent::__construct();
}
/**
* Display the view. This function wraps up all the teplates,
* sets the page title, adds all the requested Javascript and CSS,
* and passes along any data.
* @param string $view The name of the content view to display
* @param array $data (Optional) ÏAn array of any data to pass along
*/
protected function showView($view, $data = null) {
if ($data === null) {
$data = array();
}
// call all the template pieces
$this->load->view('header', $data);
$this->load->view('mainNav', $data);
$this->load->view($view, $data);
$this->load->view('footer', $data);
}
每个页面控制器在准备好显示结果或其他内容时调用$this->showView($viewName, $data);。
我的登录控制器上有一个表单,Login.php。
Login.php 有一个名为“submit”的方法。
public function submit() {
$cfg = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required|trim|alpha_numeric|xss_clean|min_length[3]|max_length[50]'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required|trim|alpha_numeric|xss_clean|min_length[3]|max_length[50]'
)
);
if ($this->form_validation->set_rules($cfg) === false) {
$this->showView("login");
} else {
$data = array(
'password' => $this->input->post('password')
);
if (filter_var($this->input->post('username'), FILTER_VALIDATE_EMAIL) === false) {
$data['username'] = $this->input->post('username');
} else {
$data['email'] = $this->input->post('username');
}
$user = $this->User->getUserFromLogin($data);
if ($user !== false) {
$sessionData = array(
'userName' => $user->userName,
'email' => $user->email,
'authToken' => $user->authToken,
'lastSeen' => date("Y-m-d")
);
// Add user data to session
$this->session->set_userdata('userLoggedIn', true);
$this->session->set_userdata('userData', $sessionData);
$this->showView("home");
} else {
$data = array(
'error_message' => 'User could not be loaded.',
);
$this->showView("login", $data);
}
}
}
我的登录视图,login.php
<div class="wrapper style1">
<article id="work">
<header>
<h2>Login!</h2>
<?=validation_errors();?>
</header>
<div class="container 50%">
<section>
<form method="post" action="login/submit">
<div>
<div class="row">
<div class="6u">
<input type="text" name="username" id="username" placeholder="username or email" value="<?=set_value('username');?>" />
</div>
<div class="6u">
<input type="password" name="password" id="password" placeholder="password" />
</div>
</div>
<div class="row">
<div class="12u">
<ul class="actions">
<li>
<input type="submit" value="Sign in!" />
</li>
</ul>
</div>
</div>
</div>
</form>
<footer>
<div>...or sign in with Facebook!</div>
<fb:login-button scope="public_profile,email" onlogin="checkLoginState();"></fb:login-button>
</footer>
</section>
</div>
</article>
</div>
成功提交表单后,我希望它会将我重定向到 home,但是,URI 是 localhost/login/submit 而不是 localhost/home。
与我的注销控制器类似,在注销时,它会导航到 URI localhost/logout/logout,这会生成 404。
我不明白为什么它没有重定向到我在showView() 方法中指定的控制器。
我没有使用任何自定义路由技巧。
【问题讨论】:
标签: php forms codeigniter-3