Php Codeigniter URI路由不工作

Php Codeigniter URI路由不工作,php,codeigniter,routing,uri,Php,Codeigniter,Routing,Uri,我在web开发中使用codeigniter框架,并使用下面的php代码传递图像id的值 <a href="<?php echo base_url('account/gallery/view'); ?>?id=<?php echo $imageDetail['imageId']; ?>"> <img src="" /> </a> 并使用此代码获取控制器上的值$imageId=$this->input->get('id') 使

我在web开发中使用codeigniter框架,并使用下面的php代码传递图像id的值

<a href="<?php echo base_url('account/gallery/view'); ?>?id=<?php echo $imageDetail['imageId']; ?>">
    <img src="" />
</a>

并使用此代码获取控制器上的值
$imageId=$this->input->get('id')

使用我使用的
get
方法,它将显示如下url:

http://my-domain-name/account/gallery/view?id=1

但我想显示如下url:

http://my-domain-name/account/gallery/1

所以我所做的是用这个代码在路由器中设置,但它不工作

$route['account/gallery/(:num)]='default/gallery\u controller/view/$1'


如果我键入
http://my-domain-name/account/gallery/1
但在我从数据库获取的字段中显示错误,因为无法获取
图像ID
请使用Regex而不是route.php中的
:num

$route['account/gallery/([0-9]+)'] = 'default/Gallery_controller/view/$1';
在模板文件中:

<a href="<?php echo base_url('account/gallery/'.$imageDetail['imageId']); ?>">
    <img src="" />
</a>
在href中正确设置此url


更换

<a href="<?php echo base_url('account/gallery/view'); ?>?id=<?php echo $imageDetail['imageId']; ?>">
检查您的config.php

$config['enable_query_strings'] = true;

在本例中,请尝试此
(:any)


在routes小写字母中

此代码使您的url类似

试试这个:

<a href="<?php echo base_url();?>account/gallery/view/<?php echo $imageDetail['imageId'];?>">
    <img src="" />
</a>


只是好奇-你是说你有$route['account/gallery/(:num)]='default/gallery\u controller/view?id=$1';工作或者,您在使用初始URL时添加了该路由吗?@TimBrownlaw我让它使用路由
$route['account/gallery/(:num)]='default/gallery\u controller/view/$1'我的网站的模板可查看,但我从数据库获取的值的所有字段显示错误,因为我设置的路由使我的网站无法获取图像的值id@LinundusOndu,检查我的答案,你能分享错误是什么吗?好的,如果你有url模式文件夹/控制器/方法/id,你可以,如前所述,设置函数视图($id){…},或者您可以使用$this->url->segment(x)获取它,其中x是段号,在您的示例中,x是4。经过所有这些努力,最终我可以使用您的解决方案。非常感谢!:)随时可以在CodeIgniter中提供帮助!!嗨Shaunak我能再问一个问题吗?如何将用户重定向到
http://my-domain-name/account/gallery/1
如果他们试图键入类似
http://my-domain-name/account/gallery/view?id=1
?我更喜欢说显示“404-找不到页面”,因为CodeIgniter不支持查询字符串和全局变量,如$\u,请使用基于SEO的URL结构。如果我们试图这样做,我们必须选择其中之一。所以最好不要重定向。另一件事,我们为什么要给用户机会操纵网站URL?
<a href="<?php echo base_url();?>account/gallery/view/<?php echo $imageDetail['imageId']; ?>">
$config['enable_query_strings'] = true;
$config['enable_query_strings'] = false;
$route['account/gallery/(:any)'] = 'default/gallery_controller/view/$1';
$route['account/gallery/view?(:any)'] = 'default/gallery_controller/view/$1';
<a href="<?php echo base_url();?>account/gallery/view/<?php echo $imageDetail['imageId'];?>">
    <img src="" />
</a>