404页面未找到codeigniter网址。

5

我是一个使用CodeIgniter的初学者。我正在使用以下URL“http://localhost/ci/index.php/shopcart”访问控制器,但是我得到了404页面未找到的错误。

控制器代码

     <?php

    class Cart extends CI_Controller { // Our Cart class extends the Controller class

        function Cart()
        {
            parent::CI_Controller(); // We define the the Controller class is the parent. 

        }


    }

        function index()
        {
            $this->load->model('cart_model'); // Load our cart model for our entire class 
            $data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products
            $data['content'] = 'cart/products'; // Select our view file that will display our products
            $this->load->view('index', $data); // Display the page with the above defined content
        }
?>

模型代码

<?php 

class Cart_model extends Model { // Our Cart_model class extends the Model class
// Function to retrieve an array with all product information
    function retrieve_products(){
        $query = $this->db->get('products'); // Select the table products
        return $query->result_array(); // Return the results in a array.
    }  

}

路由

$route['default_controller'] = "shopcart";

autoload

$autoload['libraries'] = array('cart' , 'database');
$autoload['helper'] = array('form');

http://localhost/ci/index.php/shopcart是问题所在,因为它正在寻找index.php文件夹。请尝试使用http://localhost/ci/shopcart。 - Siim Kallari
你的类名看起来像是Cart,所以你可以尝试使用 http://localhost/ci/index.php/cart 或者 http://localhost/ci/cart/。 - Shravan Sharma
http://localhost/ci/shopcart 显示“服务器上未找到/ci/shopcart请求的URL。” http://localhost/ci/index.php/cart 也显示404页面未找到。我正在使用CodeIgniter 3。 - Hammad Halim
这不是适用于CI3的好代码。这是在CI v <= 1.7.2中使用的代码。 - Tpojka
3个回答

3

CodeIgniter的基础URL是~/index.php/class_nm/function/segment3。现在在您的情况下,更改文件名Cart.php

localhost/ci/index.php/cart/index

并确保您的函数indexpublic,我猜这将解决您的问题 :)


localhost/ci/index.php/cart/index 也显示404页面未找到,我的index函数默认为公共函数,如附加在此问题中的代码所示。请注意,我的控制器文件是shopcart.php。 - Hammad Halim
change it to Cart.php - Ian Patel

1
由于控制器“shopcart”未定义,因此您会收到404页面未找到错误。相反,您已经定义了一个名为“cart”的控制器。因此,您应该尝试使用localhost/ci/index.php/cart

1
尝试添加url辅助程序,这对我很有效!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接