动态的语义 URL 路由


Dynamic, semantic URL routes

我正在 Codeigniter 中创建语义 url 路由,我从 5 个不同的表中提取数据以组合用作参数,例如;

helloworld.com/universe
helloworld.com/universe/earth
helloworld.com/universe/earth/australia
helloworld.com/universe/earth/australia/melbourne
helloworld.com/universe/earth/australia/melbourne/city

我从宇宙、世界、国家、城市和地区表中拉出行的地方。所以每个helloworld.com/(universe)helloworld.com/universe1/(planet)都写了一条路线,依此类推。

正如我们目前的数据库一样,它在我们的routes.php文件中生成了 85000 个页面作为路由。我预计在未来几个月内,这一数字将增长到数百万。

它目前的表现很好,但这种方法可持续吗?如果其路由使用包含数百万个路由的文件,它是否会影响页面加载的性能?

假设只有这些级别的路由,则可以大大减少路由中的行数.php。方法如下:

在您的路线中添加这些内容.php

$route['universe/(.*)/(.*)/(.*)/(.*)'] = 'controller/function/$1/$2/$3/$4';
$route['universe/(.*)/(.*)/(.*)'] = 'controller/function/$1/$2/$3';
$route['universe/(.*)/(.*)/'] = 'controller/function/$1/$2';
$route['universe/(.*)'] = 'controller/function/$1';

您看到的 (.*) 是一个正则表达式,表示"任何内容"。

对于 url helloworld.com/universe/earth/australia/melbourne/city ,路由将考虑$route['universe/(.*)/(.*)/(.*)/(.*)']并将您路由到controller/function/$1/$2/$3/$4其中 $1、$2、$3 和 $4 将是控制器中函数的参数。

在这里,您可以在函数中检查参数并相应地进行计算。

确保路由本身按该顺序排列,因为 CodeIgniter 假定最上面的路由具有最高优先级。