虽然这只是一种可能的解决方案的粗略想法,但您可能需要考虑定义所有受支持语言的数组。像这样的东西:
<?php
/*
Part 1 - Create an array of all the known languages. Consider making this part of the application configuration file.
*/
$languages = array(
"en",
"ge",
"pirate"
);
?>
然后检查控制器文件中的那个数组。也许使用控制器的 before 方法:
<?php
/*
Part 2 - In the controller file add a before method that checks to see if the requested language exists.
*/
public function before(){
if(!in_array($this->request->param('langauge'),$languages)):
// If the language is unknown perform a redirect.
url::redirect('DEFAULT_LANGUAGE_URL');
endif;
}
?>
转换上面 URL 结构的第一段可以使用如下代码完成:
<?php
// Get the current URI
$url = $this->request->detect_uri();
// Get the query string
// Note: If you aren't interested in preserving the query string this next line can be removed.
if($_SERVER['QUERY_STRING'] != '') $url .= '?'.$_SERVER['QUERY_STRING'];
// Set the default language. Consider setting this in your application configuration file instead.
$defaultLanguage = 'pirate';
// Replace the first URI segment with the default language.
$redirectURL = preg_replace("/^\/.*?(\/.*)$/",("/{$defaultLanguage}$1"),$url,1);
?>