Codeigniter language segments in routing

2019-05-30 10:32发布

问题:

I have this:

$route['(:any)'] = "front/$1"; // front
$route['admin/(:any)'] = "admin/$1"; // admin

i want to make url like this:

mysite.com/en/somecontroller/somemethod

mysite.com/ru/somecontroller/somemethod

etc...

i guess something like this:

$route['(:any)'] = "front/$1"; // front
$route['(:any)/(:any)'] = "front/$2";

$route['admin/(:any)'] = "admin/$1"; // admin
$route['(:any)/admin/(:any)'] = "admin/$2";

but I get errors when the first segment is language, CI thinks that is a controller

回答1:

If you are using CodeIgniter's language class, then this URI Language Identifier class extension should do exactly what you want.

This language class extension allows you to automatically prefix all site urls with a language abbreviation that is pre-defined in your config file or from a link and automatically load the corresponding language translation file, the route will then be corrected by the route regex for everything to work as normal.