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

CodeIgniter问题:登录后查看记录出现404页面未找到

Troubleshooting the 404 Error for Admin Records Page in CodeIgniter

Hey there! Let's work through this 404 issue you're hitting as a new CodeIgniter developer. I remember dealing with similar headaches when I started, so let's break down the most common fixes step by step.


1. Double-Check Your Route Configuration

CodeIgniter relies on the application/config/routes.php file to map URLs to your controller methods. If your admin records page isn't routing correctly, you'll get a 404 every time.

For example, if your admin records are handled by an Admin controller and a view_all_records method, add this line to your routes:

$route['admin/records'] = 'admin/view_all_records';
  • Make sure the URL segment (admin/records) matches what you're linking to in your header.
  • Remember: CodeIgniter is case-sensitive by default—so Admin.php (controller filename) and class Admin extends CI_Controller must match exactly.

2. Verify Your Controller & Method Exist

First, confirm your Admin.php controller is in the application/controllers folder. Inside it, you need the method to load your records, plus session validation to ensure only logged-in admins can access it:

class Admin extends CI_Controller {
    public function __construct() {
        parent::__construct();
        // Block access if user isn't logged in
        if (!$this->session->userdata('logged_in')) {
            redirect('users/login');
        }
    }

    public function view_all_records() {
        // Fetch your records here (replace with your model logic)
        $data['records'] = $this->record_model->get_all_records();
        
        // Load your template files
        $this->load->view('header');
        $this->load->view('admin/records_view', $data);
        $this->load->view('footer');
    }
}

Also, check that your server has read permissions for the controller file—sometimes file permissions can silently cause 404s.

Looking at your header snippet, your login link uses base_url(), so your admin link should follow the same pattern. Make sure you're generating the correct URL that matches your route:

<li class="nav-item">
    <a class="nav-link" href="<?php echo base_url('admin/records'); ?>">View All Records</a>
</li>

Don't forget to set your base_url correctly in application/config/config.php:

$config['base_url'] = 'http://your-website-url.com/'; // Replace with your actual domain

If base_url is empty, CodeIgniter might generate broken paths that lead to 404s.

4. Check Your .htaccess (If Using URL Rewriting)

If you're trying to remove index.php from your URLs, a misconfigured .htaccess is a common culprit. Here's a standard working configuration:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Also, ensure application/config/config.php has:

$config['index_page'] = '';

If .htaccess is missing or incorrect, CodeIgniter won't parse your URLs properly.

5. Debug with Logs

If none of the above fixes work, turn on CodeIgniter's logging to get more details. In application/config/config.php:

$config['log_threshold'] = 2; // Logs errors and messages

Then visit the 404 page, and check the application/logs folder for the latest log file. It will tell you exactly which controller/method CodeIgniter is trying to find (and failing to locate).


If you can share more code (like your full header.php, routes, or admin controller), I can help narrow it down even further. But start with these steps—they solve 90% of CodeIgniter 404 issues for new devs.

内容的提问来源于stack exchange,提问作者Harun Anwar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:22:44