我正在尝试按如下方式路由网址:
domain.com/param/some_controller/method/
映射到
some_controller::method($param)
我不熟悉正则表达式,我相当肯定这是必要的,所以如果有人能帮助我开始,我真的很感激吗?或者指点一个好的教程或例子?
答案 0 :(得分:2)
$route['(:any)/some_controller/method/'] = "some_controller/method/$1";
如果您的参数是一个数字,请使用(:num)
。
function method($my_param) {
echo 'my param is '. $my_param;
}
RL示例:
$route['(:num)/blog/entry/'] = "blog/view/$1";
class Blog extends CI_Controller {
function view($id) {
echo 'fetching blog entry no ' . $id;
}
}
您的观点将如下所示
view.php
<html>
<body>
link to <?= anchor('1/blog/entry/','my first post'); ?>
link to <?= anchor('2/blog/entry/','my second post'); ?>
</body>
</html>
答案 1 :(得分:1)
这有点乱,因为目前无法通过路由实现此目的而不会混淆事物。你可以试试这个:
$route['([^/]+)/([^/]+)/([^/]+)'] = "$2/$3/$1";
但这说明它将适用于任何网址:这会使常规控制器混淆。您最好添加一些前缀来标识您应该使用此路由处理的请求:例如
$route['a/([^/]+)/([^/]+)/([^/]+)'] = "$2/$3/$1";
答案 2 :(得分:0)
效果相同,但Codeigniter风格:
//remember that custom routes need to go _after_ ci's default routes, as they're executed in the order you provide them
$route['default_controller'] = "welcome";
$route['404_override'] = '';
//exception 1 to your regex here
//exception 2 to your regex here
//so on...
$route['(:any)/(:any)/(:any)'] = "$2/$3/$1";