2015-10-11 48 views
1

我laravel运行Web应用程序在两个不同的语言和我使用的路由设置,就像这样:翻译上班路线站点范围

Route::get('/br', function (Illuminate\Http\Request $request) { 
    return Redirect::to('/')->withCookie(cookie()->forever('locale', "pt-br")); 
}); 

这工作得很好,但对于所有不工作(myapp.com/br)

我想向用户发送他们的语言链接,使用url字符串来定义它,然后将用户重定向到页面本身,这可能是任何页面,例如:

http://www.myapp.com/pt/pricing

这会将语言设置为葡萄牙语,然后将其重定向到定价或网址中具有/ pt的任何其他页面。

基本上,我需要一个通配符检查,如果“/ PT”是URL,preferrably通过的路线。

我知道我可以用一个URL字符串做到这一点,是这样的:

http://www.myapp.com/pricing?lang=pt

但似乎有点过了我的发展格局。

所以,我需要这样的:

Route::get('/br/%anything', function (Illuminate\Http\Request $request) { 
    return Redirect::to('/%anything')->withCookie(cookie()->forever('locale', "pt-br")); 
}); 

有没有办法在laravel路由办呢?根据lang/page网址

Route::pattern('page', '[a-z]+'); 

现在,做一个重定向:

回答

1

尝试注册您的语言第一:

Route::bind('lang', function($lang){ 
    $langs = [ 
     'br' => 'pt-br', 
     'pt' => 'pt-pt', 
    ]; 

    return $langs[$lang]; 
}); 

然后添加一个模式为您的网页的标识

Route::get('{lang}/{page}', function($lang, $page){ 
    return redirect() 
      ->route($page) 
      ->withCookie(cookie()->forever('locale', $lang)); 
}); 

结果:

  • myapp.com/pt/pricing - > myapp.com/pricing(与pt在cookie中)
  • myapp.com/br/pricing - > myapp.com/pricing(与br在cookie中)
  • myapp.com/auth/login->不会重定向
+0

如果用户访问其他一些页面,例如myapp.com/auth/login,该怎么办?不会重定向到/以'auth'作为cookie登录吗? – raphadko

+0

不,它不会使用cookie重定向,因为只有url的第一部分只有两个字母才能识别{lang}'pattener。 – manix

+0

哦,我明白了..还有一件事我没有得到。在路线::模式(“页面”,“[A-Z] +”);为什么'网页'也被传递? didn/t真的得到如何模式部分工作..对不起,我有点新 – raphadko