动态,语义URL路由

时间:2014-11-18 00:29:30

标签: php codeigniter routing

我在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个页面作为路由。我预计在接下来的几个月里,这个数字会增长到数百万。

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

1 个答案:

答案 0 :(得分:2)

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

在routes.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';

你看到的(。*)是RegEx,意思是“任何东西”。

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

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

确保路由本身符合该顺序,因为CodeIgniter假设最重要的路由是最高优先级。

相关问题