CodeIgniter:向URL添加参数 [英] CodeIgniter: adding parameters to URL

查看:172
本文介绍了CodeIgniter:向URL添加参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始我的第一个CI项目,只是想知道我如何处理URL参数?我有一个名为'city'的控制器,我修改了我的mod_rewrite,所以localhost / codeigniter使用重写到localhost / codeigniter / city。我想做的是在URL的末尾添加一个城市名称,并使用get segment查询一个表。

Started my first CI project and am just wondering how I handle URL parameters? I have a controller named 'city', and I've modified my mod_rewrite so localhost/codeigniter uses rewrite to localhost/codeigniter/city. What I want to do is add a city name onto the end of the URL and use get segment to query a table.

所以我的例子是localhost / codeigniter / edinburgh。我会抓住最后一段,然后创建sql查询。但我认为当我把爱丁堡放入网址CI认为我在寻找一个控制器爱丁堡。

So my example would be localhost/codeigniter/edinburgh. I would grab the last segment and then create the sql query. However I think when I put edinburgh into the URL CI thinks I'm looking for a controller called 'edinburgh'.

我必须添加路由或类似的东西吗?

Do I have to add routing in or something similar?

推荐答案

您可以使用路由执行此操作。

You can indeed use routing to do this.


$ route [':any'] =controller / method;

$route[':any'] = "controller/method";

后面你的基地url到指定的控制器和方法里面的控制器。要获取网址细分,您可以使用 URI助手

This will redirect EVERYTHING after your base url to the specified controller and method inside that controller. To get the url segments you can use the URI helper.


$ this-> load-> helper('url'); //首先加载助手

$this->load->helper('url'); // load the helper first

$ city = $ this-> uri-> segment(1);

$city = $this->uri->segment(1);

访问 http:// localhost / codeigniter / edinburgh 时,上面示例中的$ city变量将是edinburgh 。

When accessing http://localhost/codeigniter/edinburgh the $city variable in above example would be edinburgh.

希望有所帮助!

PS。您不需要mod_rewrite指定默认控制器。你可以在你的config.php下的路由设置。指定city作为默认控制器,您可以摆脱mod_rewrite。

PS. You don't need mod_rewrite to specify a default controller. You can set it in your config.php under Routes. Specify city as your default controller and you can get rid of the mod_rewrite.

这篇关于CodeIgniter:向URL添加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆