Codeigniter Routing Regex

若如初见. 提交于 2019-12-11 01:54:46

问题


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.

Routes.php

$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; 
}

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

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 returns 'en'.
mysite.com/kr/news/page/2 // $id returns 'kr'.

It works when I write the routes out individually for each language:

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

Am I going wrong somewhere with my regex?


回答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';


来源:https://stackoverflow.com/questions/12700796/codeigniter-routing-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!