1

我有一个名为 Categories 的控制器和一个名为 index 的函数,带有 1 个参数 $cat_id

所以它看起来像这样:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class categories extends CI_Controller {

    function __construct(){
        parent::__construct();
    }

    public function index($cat_id = null){

    }
}

当我从浏览器调用它时问题就来了......我用这个:

http://www.mysite.dev/categories/12311323

但返回 404 错误页面

相反,如果我使用

http://www.mysite.dev/categories/index/12313131

会正常工作...

我如何确保它不需要索引功能的 URL 中的索引?

4

2 回答 2

6

默认的 CodeIgniter 路由是 /controller/function/argument。为了指定参数,您需要首先指定函数。如果要指定不带函数的参数,则需要定义自定义路由。将此行添加到您的路由配置文件应该可以满足您的需求。

$route['categories/(:num)'] = "categories/index/$1";
于 2012-06-02T23:05:02.160 回答
1

您描述的是 CI 的默认(预配置)路由。您可以定义自己的模式,以使您在问题中描述的模式起作用。请参阅:URI 路由

于 2012-06-02T23:01:48.990 回答