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

CodeIgniter添加购物车时触发404页面未找到错误求助

Troubleshooting the 404 Error When Adding to Cart

Hey there! Let's figure out why you're hitting that 404 when trying to add items to your cart. Based on the code snippet you shared (and since you're using CodeIgniter's base_url() helper), here are the most common fixes to check:

1. Check Your Route Configuration

CodeIgniter relies on routes to map URLs to controller methods. Head over to application/config/routes.php and make sure you have a route pointing to your cart's add function. For example, if your controller is named Cart and the method is add, add something like:

$route['cart/add/(:num)'] = 'cart/add/$1';

This tells CodeIgniter that a URL like yoursite.com/cart/add/123 should route to the add method in the Cart controller, passing the product ID (123) as a parameter.

2. Verify Your Controller & Method Exist

Double-check that:

  • Your controller file exists at application/controllers/Cart.php (note: CodeIgniter 3 requires controller filenames to start with a capital letter; adjust if you're using CI4).
  • The controller has a public add method that accepts the product ID. Example:
class Cart extends CI_Controller {
    public function add($product_id) {
        // Your add-to-cart logic here
    }
}

3. Fix the Add-to-Cart Button's URL

You shared the product details link, but I don't see the code for your add-to-cart button—this is the most likely culprit. Make sure the button's href (if using an anchor tag) or form action points to the correct route/controller method.

Example of a working anchor tag button:

<div class="col-md-4 product">
    <div class="product-price"><?php echo $product->price; ?></div>
    <a href="<?php echo base_url(); ?>products/details/<?php echo $product->id; ?>">
        <img src="<?php echo base_url(); ?>images/<?php echo $product->image; ?>" alt="<?php echo $product->name; ?>">
    </a>
    <!-- Add-to-cart button -->
    <a href="<?php echo base_url('cart/add/' . $product->id); ?>" class="btn btn-primary">Add to Cart</a>
</div>

If you're using a form (better for POST requests, ideal for modifying data like a cart), use this:

<form action="<?php echo base_url('cart/add'); ?>" method="post">
    <input type="hidden" name="product_id" value="<?php echo $product->id; ?>">
    <button type="submit" class="btn btn-primary">Add to Cart</button>
</form>

4. Confirm base_url() Is Configured Correctly

Go to application/config/config.php and ensure $config['base_url'] is set to your project's root URL. For example:

$config['base_url'] = 'http://localhost/your-shop-project/';

If this is wrong, all generated URLs will be invalid, leading to 404s.

Quick Debugging Tip

Before diving deeper, check the URL that loads when you click the add-to-cart button. Copy it from your browser's address bar—does it match the route/controller method you expect? If it's pointing to something like products/add-to-cart/1 instead of cart/add/1, that's the issue right there.

内容的提问来源于stack exchange,提问作者Kieran Fitzgerald

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:47:23