我正在一个使用多种语言URL的项目中工作,如:
mydomain/en/.....
mydomain/fr/.....
我正试图更改我的routes.php
以识别语言,并根据给定的代码设置locale,这一部分很好,但如果给定的URL没有像//mydomain/home
这样的代码,我想使用默认的区域设置重定向到//mydomain/en/home
,或者如果URL是//mydomain/
,则重定向到//mydomain/en
。
为此,我编写了代码:
$locale = Request::segment(1);
$languages = Language::getLanguages(array("enabled"=>1));
if (in_array($locale, 'Language::$available_locales)) {
'App::setLocale($locale);
} else {
'Redirect::to("//mydomain/".Config::get("lang.default_locale"));
}
Route::group(array('prefix' => $locale), function()
{
Route::get('/', function () {
return "test";
});
});
重定向程序工作不正常,有人知道如何在routes.php
中使用重定向程序吗?
上面的代码只是一个例子
我可能错了,因为我不确定你可以使用多少普通的php,但你可以使用php函数header()
而不是
'Redirect::to("//mydomain/".Config::get("lang.default_locale"));
也许:
$header_info = Config::get("lang.default_locale");
header("Location: //mydomain/$header_info");
http://php.net/manual/en/function.header.php
反斜杠可能导致Redirect
失败?对不起,我只是真的做php,但看到这个没有答案。
看起来这可能是…不在Redirect
之前的脚本:
Laravel 4:重定向到给定的url