$route['default_controller'] = "pages/index"; $route['pages/(.+)'] = 'pages/index/$1'; $route['news/(.+)'] = 'news/view/$1';
$route['default_controller'] = "pages/index"; $route['(by|ru|kz|en)/pages/(.+)'] = 'pages/index/$2'; $route['(by|ru|kz|en)/news/(.+)'] = 'news/view/$2';
<?php defined('BASEPATH') OR exit('No direct script access allowed'); /** * . * * default_key => * list => * * @author Sergey Makhlenko * @version 1.0 */ $config['ROUTE_LOCALIZE'] = array( 'default_key' => 1, // -, "list" (0 -> by, 1 -> ru, .... 4 -> en) 'list' => array('by', 'ru', 'kz', 'ua', 'en'), // );
<?php (defined('BASEPATH')) OR exit('No direct script access allowed'); // load the MX_Router class if ( file_exists(APPPATH."third_party/MX/Router.php") ) { require APPPATH."third_party/MX/Router.php"; } ....
class MY_Router extends CI_Router { /** * Language user or default language * - */ public $user_lang = ''; /** * Class constructor * * Run the route mapping function. * * @param array $routing * @return void */ public function __construct($routing = NULL) { parent::__construct(); } ... }
..... if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php')) { include(APPPATH.'config/'.ENVIRONMENT.'/routes.php'); } // Validate & get reserved routes if (isset($route) && is_array($route)) { // ... .....
// Update Routing Localize $this->__localize_init($route);
/** * Append to routing localize lang * * @param array $route Route is config/routes.php * @return array */ private function __localize_init( &$route = array() ) { // Loader config localize if (file_exists(APPPATH.'config/localize_config.php')) { include(APPPATH.'config/localize_config.php'); $localize = $config['ROUTE_LOCALIZE']; } else { return FALSE; } /* --------------------------------------------------------- */ // Check config localize if ( !isset($localize) or !isset($localize['list']) ) { return FALSE; } if ( !isset($localize['default_key']) ) { $localize['default_key'] = 0; } $localize['default_key'] = intval($localize['default_key']); /* --------------------------------------------------------- */ // Language join list $lang_list = implode('|', $localize['list']); // Create new route list foreach ( $route as $key => $item ) { $_route[$key] = $item; if ( $key == 'default_controller' ) { $_route['('.$lang_list.')'] = $route['default_controller']; $_route['('.$lang_list.')/(.+)'] = '$2'; } } /* --------------------------------------------------------- */ // Check default language if ( isset( $localize['list'][ $localize['default_key'] ] ) ) { $this->user_lang = $localize['list'][ $localize['default_key'] ]; } // User select language if ( array_search( $this->uri->segment(1), $localize['list'] ) !== FALSE ) { $this->user_lang = $this->uri->segment(1); } $route = $_route; }
$this->router->user_lang;
Source: https://habr.com/ru/post/199268/
All Articles