如何在 Kohana 中将domain.com/locale/controller路由到domain.com/controller?

发布于 2024-12-22 14:02:58 字数 1894 浏览 0 评论 0原文

我正在尝试在我的网站中实施本地化。目前,基本(英文)网站位于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深巷少女 2024-12-29 14:02:58

Kohana 按照路由在引导程序中出现的顺序应用它们。这就是为什么默认/包罗万象的路由应该始终最后定义。

来自 KO 3.0 路由 文档:

了解路由按顺序匹配很重要
它们被添加,一旦 URL 与路由匹配,路由就会被添加
基本上“停止”了,其余的路线从未尝试过。
因为默认路由几乎匹配任何内容,包括空的
url,新路由必须放在它前面。

根据建议,交换路线将解决该问题。

// This the route for the localizations:
Route::set('locale', '(<locale>(/<controller>(/<action>(/<overflow>))))', array('overflow' => '.*?'))
    ->defaults(array(
        'controller' => 'home',
        'action'     => 'index',
    ));


// This is my default route:
Route::set('default', '(<controller>(/<action>(/<overflow>)))', array('overflow' => '.*?'))
    ->defaults(array(
        'controller' => 'home',
        'action'     => 'index',
    ));

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:

It is important to understand that routes are matched in the order
they are added, and as soon as a URL matches a route, routing is
essentially "stopped" and the remaining routes are never tried.
Because the default route matches almost anything, including an empty
url, new routes must be place before it.

As suggested, swapping routes will resolve the issue.

// This the route for the localizations:
Route::set('locale', '(<locale>(/<controller>(/<action>(/<overflow>))))', array('overflow' => '.*?'))
    ->defaults(array(
        'controller' => 'home',
        'action'     => 'index',
    ));


// This is my default route:
Route::set('default', '(<controller>(/<action>(/<overflow>)))', array('overflow' => '.*?'))
    ->defaults(array(
        'controller' => 'home',
        'action'     => 'index',
    ));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文