codeigniter 路由(正则表达式)

发布于 2024-11-16 11:11:38 字数 228 浏览 0 评论 0原文

我正在尝试按如下方式路由网址:

domain.com/param/some_controller/method/

映射到

some_controller::method($param)

我不熟悉正则表达式,我相当确定这是必要的,所以如果有人可以帮助我开始,我将非常感激?或者给我指出一个好的教程或示例?

I am trying to route a url as follows:

domain.com/param/some_controller/method/

mapped to

some_controller::method($param)

I am not skilled with regular expressions, which i'm fairly certain are necessary, so I would really appreciate it if someone could help get me started? Or point me to a good tutorial or example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玩套路吗 2024-11-23 11:11:38
$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>
$route['(:any)/some_controller/method/'] = "some_controller/method/$1";

if your param is a number use (:num) instead.

in your controller

function method($my_param) {
    echo 'my param is '. $my_param;
}

an RL example:

$route['(:num)/blog/entry/'] = "blog/view/$1";

class Blog extends CI_Controller {

    function view($id) {
         echo 'fetching blog entry no ' . $id;
    }
}

your view will look like

view.php

<html>
<body>
link to <?= anchor('1/blog/entry/','my first post'); ?>
link to <?= anchor('2/blog/entry/','my second post'); ?>
</body>
</html>
划一舟意中人 2024-11-23 11:11:38

这有点混乱,因为目前还没有办法在不造成混乱的情况下通过路由来做到这一点。你可以尝试这个:

$route['([^/]+)/([^/]+)/([^/]+)'] = "$2/$3/$1";

但这意味着它将适用于任何 url:这会让常规控制器感到困惑。您最好添加一些前缀来标识您应该使用此路由处理的请求:例如

$route['a/([^/]+)/([^/]+)/([^/]+)'] = "$2/$3/$1";

This is a little messy, because there is currently no way to do this with routing without confusing things. You can try this:

$route['([^/]+)/([^/]+)/([^/]+)'] = "$2/$3/$1";

BUT what this says is that it will apply to ANY url: this will confuse regular controllers. You are better served adding some prefix to identify requests you should process with this route: for example

$route['a/([^/]+)/([^/]+)/([^/]+)'] = "$2/$3/$1";
恬淡成诗 2024-11-23 11:11:38

效果相同,但采用 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";

Same effect, but Codeigniter-style:

//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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文