2016-02-04 32 views
1

我正在使用Symfony 2.8的WebApp項目。我想爲頁面添加不同的語言。根據用戶區域設置,所有路由/ URL應該從/some/url更改爲/ locale/some/url , e.g./zh/some/url`。Symfony Routing:何時_locale屬性設置爲

添加主路由文件看起來像這樣的語言之前:

<?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"> 

    <!-- Routes for the public pages --> 
    <import 
     resource="@AppBundle/Resources/config/routing/public_routes.xml" /> 

    ... 
</routes> 

而且public_routes.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"> 

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

    <route id="public_price" path="/price" methods="GET"> 
     <default key="_controller">AppBundle:Default:price</default> 
    </route> 

    <route id="public_about" path="/about" methods="GET"> 
     <default key="_controller">AppBundle:Default:about</default> 
    </route> 

    ... 
</routes> 

到目前爲止,就這麼簡單。現在,我已經添加了以下到路由添加本地化:

<?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:Default:home</default> 
    </route> 

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

    ... 
</routes> 

所以公共頁面的進口被擴展,prefix="/{_locale}"它會自動將當前的區域從public_routes.xml所有路由。

這工作正常。我現在可以導航到/en,/en/price,/en/about

然而,仍然必須是一個「空」的路線。否則,域本身將不再有效的路由。 (BTW:有path=""path="/"之間有什麼區別?)

這是通過其控制的新home_redirect路線handeled:

public function homeAction(Request $request) { 
    $locale = $request->attributes->get('_locale'); 
    if (!$locale) { 
     // Try to get the preferred language from the request 
     $locale = MySettings::getLanguage($request);    
     return $this->redirectToRoute('home', array('_locale' => $locale)); 
    } elseif (!MySettings::checkLanguage($locale)) { 
     // Invalid Locale... 
     throw $this->createNotFoundException(); 
    } 

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

所以,如果_locale設置爲請求,homeAction檢查如果語言被支持或拋出錯誤。如果未設置_localehomeAction會嘗試從請求(例如,來自瀏覽器接受的語言)獲取當前_locale。

問題: 上述解決方案適用於所有呼叫到/。在這種情況下,_locale未被設置,因此該呼叫被重定向到/someLocale/,例如, `/ EN /。

但是:所有對/somepage的呼叫也由homeAction處理。這是因爲somepage被解釋爲語言環境!因此homeAction被調用$request->attributes->get('_locale') == 'somepage'。這顯然不能工作。

因此,如果有人呼籲舊的URL​​,而不是新/en/about,路由系統這一呼籲與路線home匹配與_locale about和頁面沒有找到。

Symfony從URL中提取_locale的位置?是否有可能攔截此提取並使其更加智能一點?例如。忽略長度超過2個字母的所有「語言環境」?

謝謝!以「FR」如果沒有_locale給予

+1

您是否嘗試過設置'requirements = {'_ locale':'en | fr | ...'}'?可能不是一個完美的解決方案,但它應該工作... –

回答

2

簡單地利用routedefinition的requirementsdefault性能

,您可以設置{_locale}的所有路由

requirements: 
    _locale: fr|en 
    defaults: { _locale: fr} 

在這個陽明例如,它的默認值例如/ 並且由於要求about不會與_locale匹配

+0

謝謝你的提示!我如何/在哪裏爲所有**路線設置了_locale?是否可以使用'config.yml'中的值而不是硬編碼'fr | en'? –

+1

肯定只是採取參數表示法:'{_locale:%locale%}' –

+0

謝謝,這工作很好! –