CodeIgniter路由已匹配却返回404错误求助
Hey there, let's break down why you're hitting that 404 even though the route seems to match. The key issue here is a missing controller method—CodeIgniter doesn't map routes directly to view files; it routes to controller methods that then load the views.
The Root Cause
Your route users/welcome is pointing to the Users controller's welcome() method, but from the code you shared, your Users controller only has a login() method. Without that welcome() method, CodeIgniter can't find what to execute, hence the 404 error—even though the view file exists.
Fix Steps
Add the
welcome()method to your Users controller
Open yourUsers.phpcontroller and add this method (you can add login validation to block direct access too):public function welcome() { // Optional: Prevent unlogged users from accessing this page if (!$this->session->userdata('logged_in')) { $this->session->set_flashdata('access_denied', 'Please log in first'); redirect('users/login'); } // Load your welcome view (matches your existing view file structure) $this->load->view('templates/header'); $this->load->view('users/welcome'); $this->load->view('templates/footer'); }Simplify Your Route Configuration (Optional)
You don't need to define a route for every single controller method. Replace your individualusers/xxxroutes with one catch-all route to keep things clean:// Replace all your users-specific routes with this $route['users/(:any)'] = 'users/$1'; $route['users'] = 'users'; // For the index methodThis will automatically map
users/welcometoUsers::welcome(),users/failedtoUsers::failed(), etc.Verify the
failed()Method Exists Too
Don't forget to add afailed()method in your Users controller as well, since you're redirecting there on login failure. It should follow the same structure as the welcome method, loading theusers/failedview.
Quick Check
After adding the method, try accessing http://mysite/users/welcome directly—this should load the page without a 404 now. The redirect from your login method will then work as expected.
内容的提问来源于stack exchange,提问作者Tim C

