Codeigniter 4提交表格引线回到同一页面
我正在尝试获取我的登录/注册表以提交。我怀疑这是因为我没有邮政路线设置,但是即使添加了这些路线,它仍然会导致同一页面。我能够直接访问页面,例如Localhost:8080/insigupcontroller/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/[email protected]/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/[email protected]/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>
登录页面中的Controller
<?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/ci4/ index.php/signin,它会给我一个404页。将baseurl更改回默认http:// localhost:8080/(这是我宁愿使用的)似乎没有解决该问题该项目根源的公共文件夹解决了该问题。现在,我在本期的问题上,登录和注册按钮似乎无能为力。我不确定这是否与问题有关,但认为我可能会添加此信息。
对此的任何帮助或指向我朝正确的方向指出,将不胜感激。我还使用XAMPP作为我的Web服务器。
I am trying to get my login/registration form to submit. I suspected it was because I did not have post routes set, but even after adding these it still leads to the same page. I am able to access the pages directly, for example localhost:8080/SignUpController/store but can't seem to get the forms to submit to these pages. Here's the code:
Sign up page
<!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/[email protected]/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>
Sign in page
<!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/[email protected]/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>
Sign in Controller
<?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');
}
}
}
Sign Up Controller
<?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
$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');
Prior to this I was having a problem where after clicking log in or sign up it would lead me to a page such as http://localhost/Ci4/index.php/signin which would give me a 404 page not found. Changing the baseURL back to the default http://localhost:8080/ (which is what I'd prefer to use) did not seem to solve that issue so after some research it seems that moving the index.php and .htaccess files from the public folder to the project's root has solved that issue. Now I'm at the present issue where the login and sign up buttons don't seem to do anything. I'm not sure if this is relevant to the issue but thought I might add this info.
Any help with this or pointing me in the right direction would be greatly appreciated. I am also using XAMPP as my web server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终重新启动了此事,并让事情开始工作。我不记得我所做的一切,但是,我相信Tsefo的答案是主要问题。
至于该页面的问题,导致“ http://localhost/ci4/index.php/signin” url是因为我设置了一个验证过滤器,以拒绝使用用户登录某些页面。不知道为什么当时没有发生这种情况...
auth.php
I ended up restarting this and got things to work. I can't recall entirely what I did, however, I believe Tsefo's answer was the primary issue.
As for the issue of the page leading to the "http://localhost/Ci4/index.php/signin" URL is because I had an Auth filter set up to deny access to certain pages if a user was not logged in. I don't know why that didn't occur to me at the time...
Auth.php
您将路线设置为“/注册”和“/signin”。在您的表单操作中,您需要使用这些URI,因此请按以下
进行
更改
方式
You have set your routes as "/signup" and "/signin". On your form actions you need to use these uris, so change as follows
to
and
to