路由中的 Symfony2 默认语言环境 [英] Symfony2 default locale in routing

查看:31
本文介绍了路由中的 Symfony2 默认语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Symfony2 构建的网站的路由和国际化方面遇到问题.
如果我在routing.yml文件中定义路由,像这样:

I have a problem with routing and the internationalization of my site built with Symfony2.
If I define routes in the routing.yml file, like this:

example: 
    pattern:  /{_locale}/example 
    defaults: { _controller: ExampleBundle:Example:index, _locale: fr } 

它适用于以下 URL:

It works fine with URLs like:

mysite.com/en/example 
mysite.com/fr/example 

但不适用于

mysite.com/example 

是不是只允许在 URL 末尾使用可选的占位符?
如果是,显示如下网址的可能解决方案是什么:

Could it be that optional placeholders are permitted only at the end of an URL ?
If yes, what could be a possible solution for displaying an url like :

mysite.com/example  

使用默认语言或将用户重定向到:

in a default language or redirecting the user to :

mysite.com/defaultlanguage/example 

当他来访时:

mysite.com/example. ? 

我正在尝试解决这个问题,但到目前为止还没有成功.

I'm trying to figure it out but without success so far.

谢谢.

推荐答案

如果有人感兴趣,我成功地在我的 routing.yml 上添加了前缀,而没有使用其他包.

If someone is interested in, I succeeded to put a prefix on my routing.yml without using other bundles.

现在,这些 URL 起作用了:

So now, thoses URLs work :

www.example.com/
www.example.com//home/
www.example.com/fr/home/
www.example.com/en/home/

编辑您的app/config/routing.yml:

ex_example:
    resource: "@ExExampleBundle/Resources/config/routing.yml"
    prefix:   /{_locale}
    requirements:
        _locale: |fr|en # put a pipe "|" first

然后,在你的app/config/parameters.yml中,你必须设置一个locale

Then, in you app/config/parameters.yml, you have to set up a locale

parameters:
    locale: en

有了这个,人们无需输入特定的语言区域就可以访问您的网站.

With this, people can access to your website without enter a specific locale.

这篇关于路由中的 Symfony2 默认语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆