codeigniter添加到购物车错误404

u3r8eeie  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(356)

我目前正试图做一个项目的基础上,youtube教程建立一个购物车,但每当我点击添加到购物车按钮,我不断得到错误404:网页找不到。
产品视图

<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; ?>" />
        </a>
        <div class="product-name"><br>
            <?php echo $product->name; ?>
        </div>
        <div class="product-add">
            <form method="post" action="<?php echo base_url(); ?> cart/add">
                QTY: <input class="qty" type="text" name="qty" value="1" />
                <input type="hidden" name="item_number" value="<?php echo $product->id; ?>"/>
                <input type="hidden" name="price" value="<?php echo $product->price; ?>"/>
                <input type="hidden" name="name" value="<?php echo $product->name; ?>"/>
                <button class="btn btn-primary" type="submit">Add To Cart</button>
            </form>

        </div>          
</div>

产品控制员

<?php
class Products extends CI_Controller{
public function index(){
    //get all products
    $data['products'] = $this->Product_model->get_products();

    //load view
    $data['main_content'] = 'products';
    $this->load->view('layouts/main', $data);
}

public function details($id){
    //get product details from the model
    $data['product'] = $this->Product_model->get_product_details($id);

    //load view
    $data['main_content'] = 'details';
    $this->load->view('layouts/main', $data);
}
}
7gs2gvoe

7gs2gvoe1#

添加以下代码
应用程序/config/ruotes.php

$route['cart/add'] = 'products/addtocart';

产品控制器-更新

<?php
class Products extends CI_Controller{
    public function __construct(){
        parent::__construct();
    }
    public function index(){
        //get all products
        $data['products'] = $this->Product_model->get_products();

        //load view
        $data['main_content'] = 'products';
        $this->load->view('layouts/main', $data);
    }
    public function details($id){
        //get product details from the model
        $data['product'] = $this->Product_model->get_product_details($id);

        //load view
        $data['main_content'] = 'details';
        $this->load->view('layouts/main', $data);
    }   
    public function addtocart(){
        // here you can collect submitted product values such as item_number, price, name

        echo '<pre>';
        print_r($_POST);        
    }
}

相关问题