2017-06-22 108 views
0

我使用CodeIgniter的框架在我的网络开发和使用PHP代码,如下笨URI路由不工作

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

通过图片ID值,并使用此代码$imageId = $this->input->get('id');

获得在控制器中的值

随着我用get方法,会显示这样的网址:

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

但我想显示的网址是这样的:

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

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

$route['account/gallery/(:num)'] = 'default/Gallery_controller/view/$1';

我的网站依然能显示网页的模板,如果我键入http://my-domain-name/account/gallery/1,但在该领域显示错误,我从数据库中获取,因为无法获得imageId

+0

只是好奇 - 你是说你有$路线[ '账户/画廊/(:NUM)'] =“缺省的/ ?Gallery_controller /视图id = $ 1' ;加工?或者在您使用初始网址后添加了该路线? – TimBrownlaw

+0

@TimBrownlaw我使用路由'$ route ['account/gallery /(:num)'] ='default/Gallery_controller/view/$ 1';'我的网站的模板可以看到,从数据库显示错误获取,因为我设置让我的网络无法获得图像ID值 –

+0

@LinundusOndu,检查我的答案,[它在这里](https://stackoverflow.com/a/44696387/2679536) –

回答

0

使用正则表达式,而不是:num在route.php

$route['account/gallery/([0-9]+)'] = 'default/Gallery_controller/view/$1'; 

而在你的模板文件:

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

然后,您可以直接在您的gallary控制器中查看功能$id

如:

class Gallery extends MY_Controller { 
    public function view($id){ 
     // Your Image ID = $id; 
    } 
} 
+0

毕竟这些努力,最后我得到它与您的解决方案。非常感谢! :) –

+0

随时帮助CodeIgniter! –

+0

嗨Shaunak,我可以问另一个问题吗?如果用户尝试输入类似“http:// my-domain-name/account/gallery/view?id =”的任何内容,我该如何将用户重定向到“http:// my- 1'? –

0

中设定这个网址http://my-domain-name/account/gallery/1在HREF

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

absolutly正确@Reena Mori –

0

适当更换

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

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

检查您的配置。PHP

$config['enable_query_strings'] = true; 

$config['enable_query_strings'] = false; 
0

在这试试这个(:any)

$route['account/gallery/(:any)'] = 'default/gallery_controller/view/$1'; 

或者

$route['account/gallery/view?(:any)'] = 'default/gallery_controller/view/$1'; 

在路线小写

0

此代码是使你的URL像http://domain-name/account/gallery/1

试试这个:

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