|
本帖最后由 gevilrror 于 2011-11-20 13:38 编辑
因为要做个支持多语言的版本,所以看了ci的语言处理方式,进行了更改
如访问地址为
http://xxx.cn/cn/xxxx
http://xxx.cn/en/xxxx
则进入不同语言版本
处理方法:
在/application/config/config 加入:
PHP复制代码 $config['languages'] = 'cn,en'; 复制代码
创建/application/core/MY_Router.php 文件
PHP复制代码 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Router extends CI_Router {
function __construct ()
{
parent ::__construct ();
}
function _parse_routes ()
{
/*----------------------------------------------------------------------------------------*/
$lang = explode(',',$this->config->item('languages'));
// Url have lang?
if(is_array($lang))
{
$lang = array_flip($lang);
if(isset($lang[$this->uri->segments[0]]))
{
$this->config->set_item('language',$this->uri->segments[0]);
$this->uri->segments = array_slice($this->uri->segments, 1);
if (count($this->uri->segments) == 0)
{
return $this->_set_default_controller ();
}
}
}
/*----------------------------------------------------------------------------------------*/
// Turn the segment array into a URI string
$uri = implode('/', $this->uri->segments);
// Is there a literal match? If so we're done
if (isset($this->routes[$uri]))
{
return $this->_set_request (explode('/', $this->routes[$uri]));
}
// Loop through the route array looking for wild-cards
foreach ($this->routes as $key => $val)
{
// Convert wild-cards to RegEx
$key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
// Does the RegEx match?
if (preg_match('#^'.$key.'$#', $uri))
{
// Do we have a back-reference?
if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
{
$val = preg_replace('#^'.$key.'$#', $val, $uri);
}
return $this->_set_request (explode('/', $val));
}
}
// If we got this far it means we didn't encounter a
// matching route so we'll set the site default route
$this->_set_request ($this->uri->segments);
}
}
/* End of file MY_Router.php */
/* Location: ./application/core/MY_Router.php */ 复制代码
欢迎有更好的方法!
|
|