【问题标题】:Inconsistent CodeIgniter routes.php behaviour不一致的 CodeIgniter routes.php 行为
【发布时间】:2015-05-11 15:54:39
【问题描述】:

我正在使用 CodeIgniter 的 routes 文件将短网址重新映射到其相关的控制器功能。

奇怪的是,一些 url 被重新映射,而另一些则没有,即使语法相同。

下面是我的routes.php 文件。 info/webinfo/rent 的重新映射按预期工作,而 info/cellinfo/hotel 被重定向到我的 404 函数。

知道是什么原因造成的吗?

$route['default_controller'] = "home";
$route['404_override'] = 'home/four_o_four';
$route['robots.txt'] = 'home/robots';

$route['info/cell'] = "articles/tags/cellphone";
$route['info/rent'] = "articles/tags/car-rental";
$route['info/web'] = "articles/tags/internet-abroad";
$route['info/hotel'] = "articles/tags/hotel";
$route['articles/tags/(:any)'] = "articles/articles_by_tags/$1";
$route['articles/destination/(:any)'] = "articles/articles_by_destination/$1";
$route['articles/(:any)'] = "articles/show_article/$1";

【问题讨论】:

  • 顺序很重要。如果不正确,较高的路线将覆盖下面的路线。
  • 我认为控制器中的问题不在路由表中。

标签: codeigniter routes


【解决方案1】:

您正在将一个 URI 重新路由到另一个重新路由的 URI。

$route['info/cell']            = "articles/tags/cellphone";
$route['info/rent']            = "articles/tags/car-rental";
$route['info/web']             = "articles/tags/internet-abroad";
$route['info/hotel']           = "articles/tags/hotel";
$route['articles/tags/(:any)'] = "articles/articles_by_tags/$1";

您应该将 URI 路由到 控制器/方法,而不是路由到另一个URI。

只需将它们全部直接路由到每个对应的控制器/方法...

$route['info/cell']            = "articles/articles_by_tags/cellphone";
$route['info/rent']            = "articles/articles_by_tags/car-rental";
$route['info/web']             = "articles/articles_by_tags/internet-abroad";
$route['info/hotel']           = "articles/articles_by_tags/hotel";
$route['articles/tags/(:any)'] = "articles/articles_by_tags/$1";

Routes Documentation

【讨论】:

  • 谢谢,斯帕基。我更改了它,它现在可以工作了,但我仍然不明白原始格式是如何出现的,它确实适用于 info/rent 和 info/web,但不适用于 info/cell 和 info/hotel...
  • @einav,你必须深入研究 CodeIgniter Core 才能弄清楚。
  • 我想我明白了。由于我应该重定向到控制器/方法,并且破折号不是类或方法名称中的有效字符,因此它不接受带有破折号的路由规则,并接受没有破折号的规则。再次感谢!
猜你喜欢
  • 1970-01-01
  • 2016-05-27
  • 2018-01-09
  • 2014-01-17
  • 1970-01-01
  • 1970-01-01
  • 2019-08-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多