Необходимо было сделать ЧПУ для мультиязычного сайта:
Типа http://example.com/index.php/language_code/controller/method/
например так: http://example.com/index.php/ru/news/list
Вот хороший способ, как это можно реализвать через хак:
Правим libraries/Router.php, вот что примерно должно получиться:
public static function setup()
{
//...
// We check this path statically, because it's overwhelmingly the most
// common path for controllers to be located at
// ADDRESS: http://example.com/controller
if (is_file(APPPATH.'controllers/'.self::$rsegments[0].EXT))
{
self::$directory = APPPATH.’controllers/’;
self::$controller = self::$rsegments[0];
self::$method = (isset(self::$rsegments[1])) ? self::$rsegments[1] : ‘index’;
self::$arguments = (isset(self::$rsegments[2])) ? array_slice(self::$rsegments, 2) : array();
}
// HACKED TO HAVE ANOTHER TWO OPTIONS for modified routing (language code included)
// ADDRESS: http://example.com/lang
elseif ( array_key_exists( self::$rsegments[0], config::item(’locale.allowed_locales’) ) AND !isset(self::$rsegments[1])) {
self::$directory = APPPATH.’controllers/’;
self::$controller = self::$routes['_default'];
self::$method = ‘index’;
// Store locale config values
self::$locales = config::item(’locale.allowed_locales’);
self::$lang = self::$rsegments[0];
Config::set(’locale.lang’, self::$lang);
Config::set(’locale.language’, self::$locales[self::$lang]);
// ADDRESS: http://example.com/lang/controller
} elseif ( array_key_exists( self::$rsegments[0], config::item(’locale.allowed_locales’) ) AND is_file(APPPATH.’controllers/’.self::$rsegments[1].EXT)) {
self::$directory = APPPATH.’controllers/’;
self::$controller = self::$rsegments[1];
self::$method = (isset(self::$rsegments[2])) ? self::$rsegments[2] : ‘index’;
self::$arguments = (isset(self::$rsegments[3])) ? array_slice(self::$rsegments, 3) : array();
// Store locale config values
self::$locales = config::item(’locale.allowed_locales’);
self::$lang = self::$rsegments[0];
Config::set(’locale.lang’, self::$lang);
Config::set(’locale.language’, self::$locales[self::$lang]);
// END OF HACK
} else {
//…
}
Два условия добавлено, чтобы разные урлы парсились правильно.
В library/Router.php прописываем два новых класса:
public static $locales = FALSE;
public static $lang = FALSE;
config/locale.php выглядит так:
'allowed_locales' => array
(
'it' => 'it_IT',
'en' => 'en_US',
'de' => 'de_DE',
'ru' => 'ru_RU',
),
И в helpers/url.php добавляем в метод site():
$url_suffix = ($uri != '') ? Config::item('core.url_suffix') : '';
$url_lang = Config::item('locale.lang').'/';
return url::base(FALSE, $protocol).$index_page.$uri.$url_suffix.$url_lang.$qs.$id;
Все!


Ответило: 0
Будь всегда в теме, подпишись на RSS ленту комментов.