简体   繁体   English

Symfony2主机URL转换

[英]Symfony2 host url translation

I am new with Symfony2 (using 2.7, because It's an existing project). 我是Symfony2的新手(使用2.7,因为它是现有项目)。 I have to create translated urls for a website with the locale in the hostname of the url. 我必须使用URL的主机名中的语言环境为网站创建翻译后的URL。

For being more clear, I will take thoses examples: 为了更清楚,我将以这些示例为例:

en.mywebsite.com/hello for US/English version or fr.mywebsite.com/bonjour for French version en.mywebsite.com/hello (美国/英语版)或fr.mywebsite.com/bonjour (法语)

I have my routing.yml file, but of course, it doesn't work. 我有我的routing.yml文件,但是它当然不起作用。

helloRoute:
   host: "{locale}.mywebsite.com"
   path: I don't know the correct input, it can be "/hello" or "/bonjour"
   defaults: {_controller: MyWebSiteBundle:Front:viewHello}
   requirements:
      locale: fr|en

There is my Nginx configuration: 有我的Nginx配置:

server {
   listen                               80;
   server_name                          ~^(?<region>\.|en|fr)\.\mywebsite.com;
    root                                /Applications/MAMP/htdocs/web/web;

    rewrite                             ^/app_dev\.php/?(.*)$ /$1 permanent;

    location / {
        index                           app_dev.php;
        try_files                       $uri @rewriteapp;
    }
    ...
}

No problem for this Nginx configuration, That works. 这个Nginx配置没问题,那行得通。

I had checked a lot of documentation, but I can't do this. 我检查了很多文档,但是我不能这样做。 The Symfony documentation is not very detailed about that. Symfony文档对此并不十分详细。

I need to have urls translated which depends on hostname locale. 我需要根据主机名的语言环境翻译URL。 Perhaps it's not possible with Symfony. Symfony可能无法实现。

Thank you in advance. 先感谢您。

I would create two routes: 我将创建两条路线:

for english 对于英语

helloRoute:
   host: "{_locale}.mywebsite.com"
   path: "/hello"
   defaults: {_controller: MyWebSiteBundle:Front:viewHello}
   requirements:
      locale: en

for french 对于法语

bonjourRoute:
   host: "{_locale}.mywebsite.com"
   path: "/bonjour"
   defaults: {_controller: MyWebSiteBundle:Front:viewHello}
   requirements:
      locale: fr

*Take note that you for got the leading underscore in the locale variable. *请注意,您在locale变量中使用了下划线。

You can send both routes to the same controller and present conditionally according to the locale. 您可以将两条路由发送到同一控制器,并根据语言环境有条件地显示。

More generally, to be able to easily handle internationalization in URL with Symfony 2, you could use BeSimple/BeSimpleI18nRoutingBundle . 更一般而言,为了能够使用Symfony 2轻松处理URL中的国际化,可以使用BeSimple / BeSimpleI18nRoutingBundle

Then, as shown in the example, you could do something like that : 然后,如示例所示,您可以执行以下操作:

helloRoute:
   host: "{locale}.mywebsite.com"
   locales:  { en: "/hello", fr: "/bonjour" }
   defaults: {_controller: MyWebSiteBundle:Front:viewHello}
   requirements:
      locale: fr|en

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM