如何在 Kohana 中将domain.com/locale/controller路由到domain.com/controller?
我正在尝试在我的网站中实施本地化。目前,基本(英文)网站位于 http://domain.com/controller/action ,我希望每个本地化位于 http://domain.com/locale/controller/action。基本上,如果用户访问后一个 URL,Kohana 将使用与英文版本相同的控制器和操作。在代码中,我将简单地交换字符串。
目前,我尝试添加以下路由,但这不起作用:
// This is my default route:
Route::set('default', '(<controller>(/<action>(/<overflow>)))', array('overflow' => '.*?'))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
// This the route for the localizations:
Route::set('locale', '(<locale>(/<controller>(/<action>(/<overflow>))))', array('overflow' => '.*?'))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
通过此设置,如果我访问 http: //domain.com/es/controller/action,我会收到 404 错误。知道我应该如何设置我的路线才能完成这项工作吗?
编辑:
只是为了完成 matino 和 John Himmelman 的答案,如果我只是按照建议交换规则,它就会起作用。但是,“locale”路由将成为包罗万象的路由,并且您总是必须指定区域设置,即使您需要的只是默认区域设置(在我的情况下为“en”/英语) )。要解决此问题,您可以将“locale”参数限制为您支持的区域设置。例如:
Route::set('locale', '(<locale>(/<controller>(/<action>(/<overflow>))))', array('locale' => '(fr|zh|en)', 'overflow' => '.*?'))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
Route::set('default', '(<controller>(/<action>(/<overflow>)))', array('overflow' => '.*?'))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
在这种情况下,仅支持以“fr”、“zh”或“en”开头的 URL。此外,不受支持的区域设置将返回 404 错误,并且“domain.com/controller/action”将正确显示默认的英语区域设置。
I'm trying to implement localization in my website. Currently, the basic (English) website is at http://domain.com/controller/action and I want each localization to be at http://domain.com/locale/controller/action. Basically, if a user visit the latter URL, Kohana will use the same controller and action than for the English version. In code, I will simply swap the strings.
Currently, I tried by adding the following route but that didn't work:
// This is my default route:
Route::set('default', '(<controller>(/<action>(/<overflow>)))', array('overflow' => '.*?'))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
// This the route for the localizations:
Route::set('locale', '(<locale>(/<controller>(/<action>(/<overflow>))))', array('overflow' => '.*?'))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
With this setup, if I visit http://domain.com/es/controller/action, I will get a 404 error. Any idea how I should setup my routes to make this work?
Edit:
Just to complete matino and John Himmelman's answer, if I simply swap the rules as suggested, it will work. However, the "locale" route would then become the catch-all route and you will always have to specify the locale, even if all you need is the default one (in my case "en" / English). To fix that, you can limit the "locale" parameter to the locales you support. For example:
Route::set('locale', '(<locale>(/<controller>(/<action>(/<overflow>))))', array('locale' => '(fr|zh|en)', 'overflow' => '.*?'))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
Route::set('default', '(<controller>(/<action>(/<overflow>)))', array('overflow' => '.*?'))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
In that case, only URLs that start with "fr", "zh" or "en" will be supported. Additionally, unsupported locales will return a 404 errors, and "domain.com/controller/action" will correctly display the default, English locale.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Kohana 按照路由在引导程序中出现的顺序应用它们。这就是为什么默认/包罗万象的路由应该始终最后定义。
来自 KO 3.0 路由 文档:
根据建议,交换路线将解决该问题。
Kohana applies routes in the order they appear in your bootstrap. This is why your default/catch-all route should always be defined last.
From KO 3.0 routing doc:
As suggested, swapping routes will resolve the issue.