Php CodeIgniter:在CodeIgniter上路由URL
我刚刚在CodeIgniter中学习了路由,现在我有点困惑了 我有这样一个URL:Php CodeIgniter:在CodeIgniter上路由URL,php,codeigniter,routes,uri,Php,Codeigniter,Routes,Uri,我刚刚在CodeIgniter中学习了路由,现在我有点困惑了 我有这样一个URL:http://localhost/norwin/list_group/get_product_by_group/1 即: 列表组是控制器 按组获取产品是一种方法 而'1'是传递参数 我想用路由最小化URL。因此,我可以有一个URL,如: http://localhost/norwin/group/'group_name' 这是我在route.php上的代码: $route['group/(:any)'] =
http://localhost/norwin/list_group/get_product_by_group/1
即:
http://localhost/norwin/group/'group_name'
这是我在route.php上的代码:
$route['group/(:any)'] = 'list_group/get_product_by_group/$1';
这是我的控制器:
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);?>
这是我在视图中调用控制器的代码:
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\u group.
也许这里有人有一个很好的方法来做这件事
感谢您的帮助。您被重定向是因为
$this->uri->segment(3)
没有将任何内容作为URL返回http://localhost/norwin/group/“组名称”
只有2个段
Codeigniter$this->uri->segment(n)代码>的工作方式如下
网址:
它会回来的
$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)
路由配置正常。你面临什么错误?@KamranAdil:结果总是将我重定向到“列表组”中,它将重定向你,因为你的URL中没有$this->uri->segment(3)
。您只有两个段“组”和“组名称”。将其更改为$this->uri->segment(2)
谢谢,我没有意识到,对不起