【问题标题】:Symfony Localization: Redirect /xyz to /en/xyzSymfony 本地化:将 /xyz 重定向到 /en/xyz
【发布时间】:2016-03-15 12:30:57
【问题描述】:

我正在使用 Symfony 2 开发一个 Web 应用程序项目。该页面将提供三种不同的语言版本。稍后将添加更多语言。

所以somepage 将在/en/somepage/fr/somepage 等下可用。

我分两步解决了这个问题:

  1. 首页/的访问者根据HTTP语言头自动重定向到本地化首页/en/fr
  2. 所有路由都用prefix="/{_locale}"导入

这是我使用的代码:

app/config/config.xml

...
parameters:
    app.default_locale: en
    app.locales: en|fr|es

src/AppBundle/Resources/config/routing.xml

<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">

    <!-- Home route to redirect to the right route -->
    <route id="home_redirect" path="" methods="GET">
        <default key="_controller">AppBundle:Public:home</default>
    </route>

    <!-- Routes for the localized public pages -->
    <import
        resource="@AppBundle/Resources/config/public_routes.xml" prefix="/{_locale}" >

        <requirement key="_locale">%app.locales%</requirement>
        <default key="_locale">%app.default_locale%</default>        
    </import>

    <!-- Routes that should not be extended by any locale -->
    <import resource="@AppBundle/Resources/config/unlocalized_routes.xml" />
</routes>

src/AppBundle/Resources/config/public_routes.xml

...

<route id="home" path="" methods="GET">
    <default key="_controller">AppBundle:Public:home</default>
</route>

<route id="public_register" path="/somepage" methods="GET">
    <default key="_controller">AppBundle:Public:register</default>
</route>

...

src/AppBundle/Controller/PublicController.php

class PublicController extends Controller {

    public function homeAction(Request $request) {
        // Check if the locale is set in the url
        $locale = $request->attributes->get('_locale');

        if (!$locale) {
            // Try to get the preferred language from the request header
            $locale = AppSettings::getLanguage($request);

            return $this->redirectToRoute('home', array('_locale' => $locale));
        }
        elseif (!AppSettings::checkLanguage($locale)) {
            // Language in URL is not supported --> Page not found
            throw $this->createNotFoundException();
        }

        return $this->render('AppBundle:Default:homepage.html.twig');
    }

}

所以访问example.com 没有问题。这将被重定向到example.com/xx,其中xx 是语言环境。此外,从public_routes.xml 导入的所有路由都会自动以区域设置为前缀。另一方面,从unlocalized_routes.xml 导入的路由仍然可以直接使用/无需区域设置。

但是不能直接访问example.com/somepage(在public_routes.xml 中定义)。必须使用受支持的语言环境,例如 example.com/en/somepage

我想让它成为可能,直接从public_routes.xml 调用所有路由(不带语言环境)并让 Symfony 处理重定向到本地化页面/路由。就像现在主页/ 一样。

当然,我可以将someroute_redirect 添加到所有公共路由的主路由文件中(就像我为主页所做的那样)。这将是可能的,但相当麻烦。我在这里寻找一个自动化的解决方案。

知道如何解决这个问题吗?

【问题讨论】:

    标签: php symfony redirect


    【解决方案1】:

    你有几个选择:

    • 创建一个操作来处​​理重定向并为其创建一个catch-all 路由并将其放入app/config/routing.yml 作为最后一个路由。您可以使用router:debug(或debug:router)命令查看订单。如果没有其他路由匹配,将执行该操作。

    • 您可以为kernel.exception 事件创建一个事件侦听器并在那里设置重定向响应

    • 使用现有捆绑包JMSI18nRoutingBundleBeSimpleI18nRoutingBundle

    我会带一个包去。

    【讨论】:

    • 感谢您的回答。我现在使用catch-all 解决方案。
    猜你喜欢
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 2014-10-25
    • 2013-08-27
    • 2016-01-21
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多