问题
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