CodeIgniter 路由 [英] CodeIgniter Routing

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

问题描述

我正在使用 CI 开发一个包含产品类别和产品的电子商务网站.我想路由 URL 以便它转到产品控制器,然后为第一个段运行 getCategoryByName 函数,然后为第二个段运行 getProductByName.这是我所拥有的:

<前>网址:产品/docupen/rc805

<前>路线.php:$route['products/([a-z]+)'] = "products/getCategoryByName/$1";$route['products/([a-z]+)/([a-z0-9]+)'] = "products/$1/getProductByName/$2";

但它不起作用.docupen"是类别,rc805"是产品.

提前致谢.

<小时>

谢谢大家的帮助.这就是我最终得到的东西.

<前>$route['products/:any/:num'] = "products/getProductByID";$route['products/:any/:any'] = "products/getProductByName";$route['products/:any'] = "products/getCategoryByName";

解决方案

我的回答建立在 Colin 的回答上.

当我在 CodeIgniter 中处理路由时,我得出的结论是路由的顺序很重要.当它找到第一个有效路由时,它不会执行列表中的其他路由.如果它没有找到任何有效的路由,那么它将处理默认路由.

我为我的特定项目使用的路线如下:

$route['default_controller'] = "main";$route['main/:any'] = "main";$route['events/:any'] = "main/events";$route['search/:any'] = "main/search";$route['events'] = "main/events";$route['search'] = "main/search";$route[':any'] = "main";

如果我输入 "http://localhost/index.php/search/1234/4321" 它将被路由到 main/search 然后我可以使用 $this->uri->segment(2); 来检索 1234.

在你的场景中,我会尝试(顺序非常重要):

$route['products/:any/:any'] = "products/getProductByName";$route['products/:any'] = "products/getCategoryByName";

我不知道如何按照您想要的方式路由 (products/$1/getProductByName/$2),但我不确定您将如何创建控制器来处理这种特定形式的URI.使用 Colin 在控制器中提到的 $this->uri->segment(n); 语句,您应该能够做您想做的事.

I am developing an ecommerce website with CI that has product categories and products. I want to route the URL so that it will go to the products controller, then run the getCategoryByName function for the first segment, then run the getProductByName for the second segment. Here is what I have:

URL:
products/docupen/rc805

routes.php:
$route['products/([a-z]+)'] = "products/getCategoryByName/$1";
$route['products/([a-z]+)/([a-z0-9]+)'] = "products/$1/getProductByName/$2";

But its not working. "docupen" is the category, and "rc805" is the product.

Thanks in advance.


Thank you all for your help. This is what I ended up with for what I needed.

$route['products/:any/:num'] = "products/getProductByID";
$route['products/:any/:any'] = "products/getProductByName";
$route['products/:any'] = "products/getCategoryByName";

解决方案

My answer builds a bit on Colin's answer.

When I played around with the routes in CodeIgniter I came to the conclusion that the order of the routes was important. When it finds the first valid route it won't do the other routes in the list. If it doesn't find any valid routes then it will handle the default route.

My routes that I played around with for my particular project worked as follows:

$route['default_controller'] = "main";
$route['main/:any'] = "main";
$route['events/:any'] = "main/events";
$route['search/:any'] = "main/search";
$route['events'] = "main/events";
$route['search'] = "main/search";
$route[':any'] = "main";

If I entered "http://localhost/index.php/search/1234/4321" It would be routed to main/search and I can then use $this->uri->segment(2); to retrieve the 1234.

In your scenario I would try (order is very important):

$route['products/:any/:any'] = "products/getProductByName";
$route['products/:any'] = "products/getCategoryByName";

I don't know enough to route the way you wanted (products/$1/getProductByName/$2), but I'm not sure how you would create a controller to handle this particular form of URI. Using the $this->uri->segment(n); statements as mentioned by Colin in your controller, you should be able to do what you want.

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

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