Codeigniter / Pyrocms中的基本路由

时间:2012-05-08 18:32:02

标签: codeigniter routing pyrocms

我在pyrocms中有一个名为event的模块,因为由于已经存在的事件类而无法将其称为事件

我想让localhost / events url引导到事件模块,所以我尝试在event / config / routes.php中设置路由

这一行

$route['events/(:any)?']        = 'event/$1';

但这不起作用 - 我做错了什么?

2 个答案:

答案 0 :(得分:6)

你需要指向类/方法,即:

$route['events/(:any)'] = 'class/method/$1';

(:any)(:num)是通配符。你可以使用自己的模式。

以此为例(出于演示目的):

// www.mysite.com/search/price/1.00-10.00
$route['search/price/(:any)'] = 'search/price/$1';

$1等于通配符(:any)

所以你可以说

public function price($range){
     if(preg_match('/(\d.+)-(\d.+)/', $range, $matches)){
         //$matches[0] = '1.00-10.00'
         //$matches[1] = '1.00'
         //$matches[2] = '10.00'
     }

     //this step is not needed however, we can simply pass our pattern into the route.
     //by grouping () our pattern into $1 and $2 we have a much cleaner controller

     //Pattern: (\d.+)-(\d.+) says group anything that is a digit and fullstop before/after the hypen( - ) in the segment 
}

现在路线变为

$route['search/price/(\d.+)-(\d.+)'] = 'search/price/$1/$2';

控制器

public function price($start_range, $end_range){}

答案 1 :(得分:1)

我认为问号可能会干扰路由,所以它应该是:

$route['events/(:any)'] = 'event/$1';