Codeigniter Routes Regex中的两个

时间:2015-08-30 08:28:24

标签: regex codeigniter routes multiple-languages

我想要

en/careers to career
fa/careers to career
careers to career

我尝试了$route['(en|fa)(.+)/careers'] = 'career';

但它不起作用

2 个答案:

答案 0 :(得分:0)

en/fa/可选:

(en/|fa/)?careers

答案 1 :(得分:0)

我会在Routes.php中使用这个正则表达式

$route['en/careers|fa/careers|careers'] = 'career/index';

图解说明:

enter image description here

<强>更新

但我认为发送语言字母会更有意义#34; en&#34;或&#34; fa&#34;作为职业控制人员/方法进行测试等的论据,所以它看起来像这样:

www.example.com/en/careers = www.example.com/careers/lng/en
www.example.com/fa/careers = www.example.com/careers/lng/fa
www.example.com/careers = www.example.com/careers/lng/en 
//(assuming that en is the default language if no language was specified)

如果这样做是敏感的那么你应该去那样的3条规则

$route['(en)/careers'] = 'career/lng/$1';
$route['(fa)/careers'] = 'career/lng/$1';
$route['careers'] = 'career/lng/en';

这里是lree()方法,它位于careers.php

 public function lng($lang_id)
 {
    if($lang_id == "en")  
        echo "welcome"; 
    elseif($lang_id == "fa")
        echo "خوش آمد";  // not sure if this is correct in "fa" language :p
 }

注意:如果您的语言中有id存储在数据库中,您可以将$ 1更改为数字ID

$route['en/careers'] = 'career/lng/1';
$route['fa/careers'] = 'career/lng/2';
$route['careers'] = 'career/lng/1';

那么你的lng()方法将如下所示

 public function lng($lang_id)
 {
    if($lang_id == 1)  
        echo "welcome"; 
    elseif($lang_id == 2)
        echo "خوش آمد";  // not sure if this is correct in "fa" language :p
 }

我希望有帮助