CodeIgniter 4 提交表单导致返回同一页面
CodeIgniter 4 Submit form leads back to the same page
我正在尝试提交我的 login/registration 表单。我怀疑是因为我没有设置 post 路由,但即使添加了这些路由,它仍然会指向同一页面。我可以直接访问这些页面,例如 localhost:8080/SignUpController/store 但似乎无法获取要提交到这些页面的表单。这是代码:
注册页面
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
<title>Sign Up</title>
</head>
<body>
<div class="container mt-5">
<div class="row justify-content-md-center">
<div class="col-5">
<h2>Register User</h2>
<?php if(isset($validation)):?>
<div class="alert alert-warning">
<?= $validation->listErrors() ?>
</div>
<?php endif;?>
<form action="<?php echo base_url(); ?>/SignupController/store" method="post">
<div class="form-group mb-3">
<input type="text" name="name" placeholder="Name" value="<?= set_value('name') ?>" class="form-control" >
</div>
<div class="form-group mb-3">
<input type="email" name="email" placeholder="Email" value="<?= set_value('email') ?>" class="form-control" >
</div>
<div class="form-group mb-3">
<input type="password" name="password" placeholder="Password" class="form-control" >
</div>
<div class="form-group mb-3">
<input type="password" name="confirmpassword" placeholder="Confirm Password" class="form-control" >
</div>
<div class="d-grid">
<button type="submit" class="btn btn-dark">Signup</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
登录页面
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
<title>Sign In</title>
</head>
<body>
<div class="container">
<div class="row justify-content-md-center">
<div class="col-5">
<h2>Login</h2>
<?php if(session()->getFlashdata('msg')):?>
<div class="alert alert-warning">
<?= session()->getFlashdata('msg') ?>
</div>
<?php endif;?>
<form action="<?php echo base_url(); ?>/SigninController/loginAuth" method="post">
<div class="form-group mb-3">
<input type="email" name="email" placeholder="Email" value="<?= set_value('email') ?>" class="form-control" >
</div>
<div class="form-group mb-3">
<input type="password" name="password" placeholder="Password" class="form-control" >
</div>
<div class="d-grid">
<button type="submit" class="btn btn-success">Sign in</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
登录控制器
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\UserModel;
class SignInController extends Controller
{
public function index()
{
helper(['form']);
echo view('signin');
}
public function loginAuth()
{
$session = session();
$userModel = new UserModel();
$email = $this->request->getVar('email');
$password = $this->request->getVar('password');
$data = $userModel->where('email', $email)->first();
if($data){
$pass = $data['password'];
$authenticatePassword = password_verify($password, $pass);
if($authenticatePassword){
$ses_data = [
'id' => $data['id'],
'name' => $data['name'],
'email' => $data['email'],
'isLoggedIn' => TRUE
];
$session->set($ses_data);
return redirect()->to('/profile');
}else{
$session->setFlashdata('msg', 'Password is incorrect.');
return redirect()->to('/signin');
}
}else{
$session->setFlashdata('msg', 'Email does not exist.');
return redirect()->to('/signin');
}
}
}
注册控制器
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\UserModel;
class SignUpController extends Controller
{
public function index()
{
helper(['form']);
$data = [];
echo view('signup', $data);
}
public function store()
{
helper(['form']);
$rules = [
'name' => 'required|min_length[2]|max_length[50]',
'email' => 'required|min_length[4]|max_length[100]|valid_email|is_unique[users.email]',
'password' => 'required|min_length[4]|max_length[50]',
'confirmpassword' => 'matches[password]'
];
if($this->validate($rules)){
$userModel = new UserModel();
$data = [
'name' => $this->request->getVar('name'),
'email' => $this->request->getVar('email'),
'password' => password_hash($this->request->getVar('password'), PASSWORD_DEFAULT)
];
$userModel->save($data);
return redirect()->to('signin');
}else{
$data['validation'] = $this->validator;
echo view('signup', $data);
}
}
}
路线
$routes->get('/', 'SignupController::index');
$routes->post('/', 'SignupController::store');
$routes->get('/signup', 'SignUpController::index');
$routes->post('/signup', 'SignUpController::store');
$routes->get('/signin', 'SignInController::index');
$routes->post('/signin', 'SignInController::loginAuth');
在此之前,我遇到了一个问题,点击登录或注册后,它会将我带到一个页面,例如 http://localhost/Ci4/index.php/signin,这会给我一个 404找不到网页。将 baseURL 改回默认的 http://localhost:8080/(这是我更喜欢使用的)似乎并没有解决这个问题所以经过一些研究似乎移动 index.php 和从 public 文件夹到项目根目录的 .htaccess 文件解决了这个问题。现在我遇到了登录和注册按钮似乎没有任何作用的问题。我不确定这是否与问题相关,但我想我可以添加此信息。
如能提供任何帮助或为我指明正确的方向,我们将不胜感激。我还使用 XAMPP 作为我的网络服务器。
您已将路由设置为“/signup”和“/signin”。在您的表单操作中您需要使用这些 uris,因此更改如下
action="<?php echo base_url(); ?>/SignupController/store"
到
action="<?php echo base_url(); ?>/signup"
和
action="<?php echo base_url(); ?>/SigninController/loginAuth"
到
action="<?php echo base_url(); ?>/signin"
我最终重新启动了它,一切正常。我不记得我做了什么,但是,我相信 Tsefo 的回答是主要问题。
至于导致“http://localhost/Ci4/index.php/signin”页面的问题 URL 是因为我设置了 Auth 过滤器以拒绝访问某些页面,如果用户未登录。我不知道为什么我当时没有想到...
Auth.php
<?php
namespace App\Filters;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;
class Auth implements FilterInterface
{
public function before(RequestInterface $request, $arguments = null)
{
// if user not logged in
if(! session()->get('logged_in')) {
// then redirct to login page
return redirect()->to('/login');
}
}
//--------------------------------------------------------------------
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
// Do something here
}
}
我正在尝试提交我的 login/registration 表单。我怀疑是因为我没有设置 post 路由,但即使添加了这些路由,它仍然会指向同一页面。我可以直接访问这些页面,例如 localhost:8080/SignUpController/store 但似乎无法获取要提交到这些页面的表单。这是代码:
注册页面
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
<title>Sign Up</title>
</head>
<body>
<div class="container mt-5">
<div class="row justify-content-md-center">
<div class="col-5">
<h2>Register User</h2>
<?php if(isset($validation)):?>
<div class="alert alert-warning">
<?= $validation->listErrors() ?>
</div>
<?php endif;?>
<form action="<?php echo base_url(); ?>/SignupController/store" method="post">
<div class="form-group mb-3">
<input type="text" name="name" placeholder="Name" value="<?= set_value('name') ?>" class="form-control" >
</div>
<div class="form-group mb-3">
<input type="email" name="email" placeholder="Email" value="<?= set_value('email') ?>" class="form-control" >
</div>
<div class="form-group mb-3">
<input type="password" name="password" placeholder="Password" class="form-control" >
</div>
<div class="form-group mb-3">
<input type="password" name="confirmpassword" placeholder="Confirm Password" class="form-control" >
</div>
<div class="d-grid">
<button type="submit" class="btn btn-dark">Signup</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
登录页面
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
<title>Sign In</title>
</head>
<body>
<div class="container">
<div class="row justify-content-md-center">
<div class="col-5">
<h2>Login</h2>
<?php if(session()->getFlashdata('msg')):?>
<div class="alert alert-warning">
<?= session()->getFlashdata('msg') ?>
</div>
<?php endif;?>
<form action="<?php echo base_url(); ?>/SigninController/loginAuth" method="post">
<div class="form-group mb-3">
<input type="email" name="email" placeholder="Email" value="<?= set_value('email') ?>" class="form-control" >
</div>
<div class="form-group mb-3">
<input type="password" name="password" placeholder="Password" class="form-control" >
</div>
<div class="d-grid">
<button type="submit" class="btn btn-success">Sign in</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
登录控制器
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\UserModel;
class SignInController extends Controller
{
public function index()
{
helper(['form']);
echo view('signin');
}
public function loginAuth()
{
$session = session();
$userModel = new UserModel();
$email = $this->request->getVar('email');
$password = $this->request->getVar('password');
$data = $userModel->where('email', $email)->first();
if($data){
$pass = $data['password'];
$authenticatePassword = password_verify($password, $pass);
if($authenticatePassword){
$ses_data = [
'id' => $data['id'],
'name' => $data['name'],
'email' => $data['email'],
'isLoggedIn' => TRUE
];
$session->set($ses_data);
return redirect()->to('/profile');
}else{
$session->setFlashdata('msg', 'Password is incorrect.');
return redirect()->to('/signin');
}
}else{
$session->setFlashdata('msg', 'Email does not exist.');
return redirect()->to('/signin');
}
}
}
注册控制器
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\UserModel;
class SignUpController extends Controller
{
public function index()
{
helper(['form']);
$data = [];
echo view('signup', $data);
}
public function store()
{
helper(['form']);
$rules = [
'name' => 'required|min_length[2]|max_length[50]',
'email' => 'required|min_length[4]|max_length[100]|valid_email|is_unique[users.email]',
'password' => 'required|min_length[4]|max_length[50]',
'confirmpassword' => 'matches[password]'
];
if($this->validate($rules)){
$userModel = new UserModel();
$data = [
'name' => $this->request->getVar('name'),
'email' => $this->request->getVar('email'),
'password' => password_hash($this->request->getVar('password'), PASSWORD_DEFAULT)
];
$userModel->save($data);
return redirect()->to('signin');
}else{
$data['validation'] = $this->validator;
echo view('signup', $data);
}
}
}
路线
$routes->get('/', 'SignupController::index');
$routes->post('/', 'SignupController::store');
$routes->get('/signup', 'SignUpController::index');
$routes->post('/signup', 'SignUpController::store');
$routes->get('/signin', 'SignInController::index');
$routes->post('/signin', 'SignInController::loginAuth');
在此之前,我遇到了一个问题,点击登录或注册后,它会将我带到一个页面,例如 http://localhost/Ci4/index.php/signin,这会给我一个 404找不到网页。将 baseURL 改回默认的 http://localhost:8080/(这是我更喜欢使用的)似乎并没有解决这个问题所以经过一些研究似乎移动 index.php 和从 public 文件夹到项目根目录的 .htaccess 文件解决了这个问题。现在我遇到了登录和注册按钮似乎没有任何作用的问题。我不确定这是否与问题相关,但我想我可以添加此信息。
如能提供任何帮助或为我指明正确的方向,我们将不胜感激。我还使用 XAMPP 作为我的网络服务器。
您已将路由设置为“/signup”和“/signin”。在您的表单操作中您需要使用这些 uris,因此更改如下
action="<?php echo base_url(); ?>/SignupController/store"
到
action="<?php echo base_url(); ?>/signup"
和
action="<?php echo base_url(); ?>/SigninController/loginAuth"
到
action="<?php echo base_url(); ?>/signin"
我最终重新启动了它,一切正常。我不记得我做了什么,但是,我相信 Tsefo 的回答是主要问题。
至于导致“http://localhost/Ci4/index.php/signin”页面的问题 URL 是因为我设置了 Auth 过滤器以拒绝访问某些页面,如果用户未登录。我不知道为什么我当时没有想到...
Auth.php
<?php
namespace App\Filters;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;
class Auth implements FilterInterface
{
public function before(RequestInterface $request, $arguments = null)
{
// if user not logged in
if(! session()->get('logged_in')) {
// then redirct to login page
return redirect()->to('/login');
}
}
//--------------------------------------------------------------------
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
// Do something here
}
}