Codeigniter v.3自定义路线

时间:2017-04-25 12:06:08

标签: php codeigniter routing

我使用codeigniter v.3,我的大部分页面都有以下网址:

www.domain.buu/page/index/105

其中Page是Controller Name,index是函数的名称,105是记录的id。

我想要的是www.domain.buu/my_custom_page_name 其中my_custom_page_name可以是routes.php

中的内容
105 = "my_custom_page_name"
106 = "my_other_custom_page_name"
107 = "some_other_page_name"

这可能吗? CodeIgniter手册对此没有多大帮助。我尝试使用以下代码更改路线:

$route['index/page/:num'] = 'my_custom_page_name';

但它没有用。

3 个答案:

答案 0 :(得分:4)

你在尝试路线时走在正确的轨道上,但你需要翻转参数。

$route中的数组键是用户将看到的URI,例如您的my_custom_page_name
值是您要使用的控制器和方法,例如page / index / 105

在您的情况下,路线可能是这样的:

$route['my_custom_page_name'] = 'index/page/105';

答案 1 :(得分:3)

如果您尝试实施易于使用的URL,则可以在数据库中添加名为slug

的其他列,使用slug代替105(假设其唯一ID)

例如,http://www.example.com/page/this-is-my-slug/

使用routes.php时,您需要复制所有条目的路由设置。

$route['my_custom_page_name'] = 'index/page/105';
$route['my_custom_page_name-new'] = 'index/page/106';

答案 2 :(得分:2)

我不确定你想要取得什么,但也许这就是:

$route['my_custom_page_name/(:num)'] = 'page/index/$1';