Codeigniter路由正则表达式 [英] Codeigniter Routing Regex

查看:112
本文介绍了Codeigniter路由正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用多种语言的网站。对于我的新闻页面,我有两个规则将分页变量路由到我的控制器。一种用于所有语言(en,ct,cs,kr),另一种用于默认语言。

I have a site with multiple languages. For my news page I have two rules to route the pagination variable to my controller. One for all the languages (en, ct, cs, kr), and one for the default language.

$route['^(en|ct|cs|kr)/news/page/(:num)'] = 'news/index/$1';
$route['news/page/(:num)'] = 'news/index/$1';



新闻管理员



News controller

public function index($id) 
{
   echo $id; 
}

路由正在访问新闻控制器,但是 $ id 参数未传递给 index()方法。

The routes are accessing the news controller, however the $id parameter isn't being passed to the index() method.

如果我回显 $ id ,它将返回语言段而不是我期望的分页变量:

If I echo the $id it returns the language segment rather than the pagination variable I am expecting:


mysite.com/en/news/page/2 // $ id返回'en'。

mysite.com / kr / news / page / 2 // $ id返回'kr'。

mysite.com/en/news/page/2 // $id returns 'en'.
mysite.com/kr/news/page/2 // $id returns 'kr'.

当我分别为每种语言:


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

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

我正则表达式的某个地方出错了吗?

Am I going wrong somewhere with my regex?

推荐答案

这是因为在您的第一个规则中,您正在捕获URL的2个段。第一个是语言(例如 en ),第二个是 id (或页码)。因此,在第一个规则中,您应该使用 $ 2 而不是 $ 1

That's because in your first rule, you're capturing 2 segments of the URL. The first one is the language (e.g. en), and the second one is the id (or page number). So, in your first rule you should instead use $2 instead of $1.

$route['^(en|ct|cs|kr)/news/page/(:num)'] = 'news/index/$2';

这篇关于Codeigniter路由正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆