我使用codeigniter4验证特性对后处理进行了如下调整:
class Auth extends BaseController
{
protected $validation;
protected $db;
protected $email;
public function __construct()
{
date_default_timezone_set('Asia/Jakarta');
$this->validation = ConfigServices::validation();
$this->db = ConfigDatabase::connect();
$this->email = ConfigServices::email();
}
function index()
{
$this->goToDefaultPage();
if (isset($_POST['login'])) {
$this->validation->setRule('nip', 'NIP', 'required|min_length[18]|max_length[18]|trim', ['required' => 'NIP tidak boleh kosong', 'min_length' => 'NIP harus 18 digit', 'max_length' => 'NIP harus 18 digit']);
if (!$this->validation->withRequest($this->request)->run()) {
return redirect()->to('sitkd/auth')->withInput();
$this->validation->getError('nip');
} else {
$this->_login();
}
}
$data = [
'title' => 'Login | SITKD JATENG',
'validation' => $this->validation
];
echo view('sitkd/templates/auth_header', $data);
echo view('sitkd/auth/login');
echo view('sitkd/templates/auth_footer');
}
function _login()
{
$builder = $this->db->table('user');
$nip = $this->request->getPost('nip');
$password = $this->request->getPost('password');
$user = $builder->getWhere(['nip' => $nip])->getRowArray();
if ($user) {
if ($user['is_active'] == 1) {
if (password_verify($password, $user['password'])) {
$data = [
'email' => $user['email'],
'nip' => $user['nip'],
'role_id' => $user['role_id'],
'user_id' => $user['user_id'],
'permendagri_id' => $user['permendagri_id'],
];
session()->set($data);
if ($user['role_id'] == 1) {
return redirect()->to(site_url('sitkd/admin'));
}
if ($user['role_id'] == 2) {
return redirect()->to(site_url('sitkd/moderator'));
}
if ($user['role_id'] == 3) {
return redirect()->to(site_url('sitkd/member'));
}
} else {
session()->setFlashdata('message', '<div class="alert alert-danger" role="alert">Password/NIP tidak terdaftar</div>');
return redirect('sitkd/auth');
}
} else {
session()->setFlashdata('message', '<div class="alert alert-danger" role="alert">Akun belum diaktifkan. Mohon hubungi admin / moderator!</div>');
return redirect('sitkd/auth');
}
} else {
session()->setFlashdata('message', '<div class="alert alert-danger" role="alert">NIP belum terdaftar. Silahkan registrasi</div>');
return redirect('sitkd/auth');
}
}
function goToDefaultPage()
{
if (session()->set('role_id') == 1) {
return redirect('sitkd/admin');
} else if (session()->set('role_id') == 2) {
return redirect('sitkd/moderator');
} else if (session()->set('role_id') == 3) {
return redirect('sitkd/member');
}
}
}
下面是我创建的视图:
<?= session()->getFlashdata('message'); ?>
<form class="user" method="post" action="<?= base_url('sitkd/auth'); ?>">
<div class="form-group">
<input type="text" class="auth-control auth-control-user is-invalid" id="nip" name="nip" autocomplete="off" placeholder="Masukan NIP" value="<?= old('nip'); ?>">
<div class="invalid-feedback ml-3">
<?= $validation->getError('nip') ?>
</div>
</div>
<hr>
<div class="form-group">
<input type="password" class="auth-control auth-control-user" id="password" autocomplete="off" name="password" placeholder="Password">
</div>
<hr>
<button type="submit" name="login" class="btn btn-login btn-user btn-block">
Login
</button>
</form>
验证特性工作得很好然后我创建的路由是这样的:
$routes->get('/sitkd/auth', 'SitkdAuth::index');
$routes->get('/sitkd/admin', 'SitkdAdmin::index');
,如果登录成功,这是控制器和方法:
class Admin extends BaseController
{
protected $db;
public function __construct()
{
date_default_timezone_set('Asia/Jakarta');
$this->db = ConfigDatabase::connect();
}
public function index()
{
echo "halo admin";
}
}
当我在主控制器上运行index (sitkd/index)时,一切都运行顺利。但是,当具有admin状态的用户成功登录时,为什么返回的视图是"登录视图"?而不是"管理视图"。帮我找条出路,怎么了?我真的很感激你的帮助,谢谢。
像我一样验证代码参见我的例子
$rules = [
'name' => 'if_exist|required|min_length[3]|max_length[255]',
'description' => 'required|min_length[3]|max_length[255]',
];
if (!$this->validate($rules)) {
$this->_login();
}