You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter路由已匹配却返回404错误求助

Troubleshooting Your CodeIgniter 404 After Login Redirect

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

  1. Add the welcome() method to your Users controller
    Open your Users.php controller 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');
    }
    
  2. Simplify Your Route Configuration (Optional)
    You don't need to define a route for every single controller method. Replace your individual users/xxx routes 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 method
    

    This will automatically map users/welcome to Users::welcome(), users/failed to Users::failed(), etc.

  3. Verify the failed() Method Exists Too
    Don't forget to add a failed() 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 the users/failed view.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:13:11