ARTICLE AD BOX
I am developing a login system using CodeIgniter 4 and PHP 8.
When I access the login page via GET (/auth/login), the view loads correctly. However, when I submit the form using POST to the same URL, I receive a 404 Not Found error.
I have checked my controller and view names for case sensitivity (since I am on a Linux server), but I'm stuck.
My Controller (app/Controllers/Auth.php):
<?php namespace App\Controllers; use App\Models\UserModel; use App\Models\LoginHistoryModel; class Auth extends BaseController { public function index() { return view('login'); } public function login() { date_default_timezone_set('Asia/Jakarta'); // Check if request is POST if ($this->request->getMethod() === 'post') { $session = session(); $model = new UserModel(); $historyModel = new LoginHistoryModel(); // Get input (Using array format 'auth[nik]') $nik = $this->request->getPost('auth[nik]'); $password = $this->request->getPost('auth[password]'); $user = $model->checkLogin($nik, $password); if ($user) { $sessionData = [ 'id' => $user['id'], 'nik' => $user['nik'], 'name' => $user['name'], 'role' => $user['role'], 'department' => $user['department'] ?? 'General', 'logged_in' => TRUE ]; // Save Login History try { $historyId = $historyModel->insert([ 'nik' => $user['nik'], 'name' => $user['name'], 'ip_address' => $this->request->getIPAddress(), 'last_activity' => 'User Login' ]); $sessionData['login_history_id'] = $historyId; $session->set($sessionData); return redirect()->to('/welcome')->with('success', 'Welcome'); } catch (\Exception $e) { log_message('error', 'History Error: ' . $e->getMessage()); return redirect()->to('/welcome'); } } else { $session->setFlashdata('error', 'NIK or Password incorrect!'); return redirect()->to('/'); } } return redirect()->to('/'); } public function logout() { session()->destroy(); return redirect()->to('/'); } public function welcome() { if (!session()->get('logged_in')) { return redirect()->to('/'); } return view('welcome'); } } <form action="<?= base_url('auth/login') ?>" method="post"> <?= csrf_field() ?> <input type="text" name="auth[nik]" placeholder="NIK"> <input type="password" name="auth[password]" placeholder="Password"> <button type="submit">Login</button> </form>this for form login
and this :
app/Config/Routes.php
I cant go to login page, index page, i use CI 4 you can download
https://drive.google.com/drive/folders/1YN42LDbo3frCAXCP5clp7hMp5xt4rX8F?usp=sharing
please help me to solve this
