CodeIgniter:在 Codeigniter 上路由 URL

CodeIgniter : Routing URL on Codeigniter

我刚刚了解了 CodeIgniter 中的路由,现在我有点困惑。

我有一个 URL 这样的:http://localhost/norwin/list_group/get_product_by_group/1

即:

  1. list_group是控制者,

  2. get_product_by_group是方法,

  3. '1'正在传递参数。

我想用路线最小化 URL。所以我可以有一个 URL 像 :

http://localhost/norwin/group/'group_name'

这是我在 route.php 上的代码:

$route['group/(:any)'] = 'list_group/get_product_by_group/';

这是我的控制器:

    public function get_product_by_group($group_name)
    {
         
            if($this->uri->segment(3))
            {
                    $this->load->database();
                    $data['product_data'] = $this->group_model->list_product_by_group($group_name);
                    $this->load->view('fend/list_product', $data);

            }
            else
            {
                    redirect('list_group');
            }

    }

这是我调用控制器的视图代码:

<?= base_url('group/'. $value->group_name);?>

我的问题是:我得不到路线的任何结果,它总是把我送到 'list_group'.

也许这里有人有好的方法。

感谢您的帮助。

您被重定向是因为 $this->uri->segment(3) 没有 returning 任何东西,因为您的 URL http://localhost/norwin/group/'group_name' 只有 2 个片段。

Codeigniter $this->uri->segment(n); 以下列方式工作。

URL: http://localhost/norwin/group/group_name/some_test

会return

$this->uri->segment(1); // group
$this->uri->segment(2); // group_name
$this->uri->segment(3); // some_test

您必须将 $this->uri->segment(3) 更改为 $this->uri->segment(2)