Symfony2 路由中的默认语言环境
我在使用 Symfony2 构建的网站的路由和国际化方面遇到问题。
如果我在routing.yml 文件中定义路由,如下所示:
example:
pattern: /{_locale}/example
defaults: { _controller: ExampleBundle:Example:index, _locale: fr }
它适用于以下 URL:
mysite.com/en/example
mysite.com/fr/example
但不适用于
mysite.com/example
是否仅允许在 URL 末尾使用可选占位符?
如果是,那么可能的解决方案是什么,可以用
mysite.com/example
默认语言显示 : 之类的 url,或者
mysite.com/defaultlanguage/example
当用户访问 : 时将用户重定向到 :
mysite.com/example. ?
我正在尝试解决这个问题,但到目前为止还没有成功。
谢谢。
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 }
It works fine with URLs like:
mysite.com/en/example
mysite.com/fr/example
But doesn't work with
mysite.com/example
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
when he visits :
mysite.com/example. ?
I'm trying to figure it out but without success so far.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
如果有人感兴趣,我成功地在我的
routing.yml
上添加了前缀,而无需使用其他包。现在,这些 URL 可以工作了:
编辑您的
app/config/routing.yml
:然后,在您的
app/config/parameters.yml
中,您必须设置一个区域设置这样,人们就可以访问您的网站,而无需输入特定的区域设置。
If someone is interested in, I succeeded to put a prefix on my
routing.yml
without using other bundles.So now, thoses URLs work :
Edit your
app/config/routing.yml
:Then, in you
app/config/parameters.yml
, you have to set up a localeWith this, people can access to your website without enter a specific locale.
您可以像这样定义多个模式:
您应该能够使用注释实现同样的事情:
希望有帮助!
You can define multiple patterns like this:
You should be able to achieve the same sort of thing with annotations:
Hope that helps!
这就是我用于自动区域设置检测和重定向的方法,它运行良好并且不需要冗长的路由注释:
routing.yml
locale
路由处理网站的根目录,然后每个其他控制器操作都以区域设置为前缀。CoreController.php
这会检测用户的语言并重定向到您选择的路线。我使用 home 作为默认值,因为这是最常见的情况。
然后,路由注释可以简单地是:
Twig
localeAction 可用于允许用户更改区域设置,而无需离开当前页面:
Clean &简单的!
This is what I use for automatic locale detection and redirection, it works well and doesn't require lengthy routing annotations:
routing.yml
The
locale
route handles the website's root and then every other controller action is prepended with the locale.CoreController.php
This detects the user's language and redirects to the route of your choice. I use home as a default as that it the most common case.
Then, the route annotations can simply be:
Twig
The localeAction can be used to allow the user to change the locale without navigating away from the current page:
Clean & simple!
Joseph Astrahan 的 LocalRewriteListener 解决方案除了带参数的路由外都有效,因为
$routePath == "/{_locale}".$path)
例如:
$routePath = "/{_locale}/my /route/{foo}"
与$path = "/{_locale}/my/route/bar"
不同,我必须使用 UrlMatcher (链接到 Symfony 2.7 api 文档)用于匹配实际带有 url 的路线。
我更改了 isLocaleSupported 以使用浏览器本地代码(例如:fr -> fr_FR)。我使用浏览器区域设置作为键,使用路由区域设置作为值。我有一个像这样的数组
array(['fr_FR'] => ['fr'], ['en_GB'] => 'en'...)
(请参阅下面的参数文件了解更多信息)更改:
这是我的代码。适用于任何带或不带参数的路线。仅当在路由中设置了 {_local} 时,才会添加区域设置。
路由文件(在我的例子中是app/config中的)
app/config/parameters.yml文件中的参数
services.yml
<强>LocaleRewriteListener.php
The Joseph Astrahan's solution of LocalRewriteListener works except for route with params because of
$routePath == "/{_locale}".$path)
Ex :
$routePath = "/{_locale}/my/route/{foo}"
is different of$path = "/{_locale}/my/route/bar"
I had to use UrlMatcher (link to Symfony 2.7 api doc) for matching the actual route with the url.
I change the isLocaleSupported for using browser local code (ex : fr -> fr_FR). I use the browser locale as key and the route locale as value. I have an array like this
array(['fr_FR'] => ['fr'], ['en_GB'] => 'en'...)
(see the parameters file below for more information)The changes :
Here is my code. Works for any route with or without param. This add the locale only when {_local} is set in the route.
Routing file (in my case, the one in app/config)
The parameter in app/config/parameters.yml file
services.yml
LocaleRewriteListener.php
交响乐3
Symfony3
这是我的解决方案,它使这个过程更快!
控制器:
树枝:
There is my Solution, it makes this process faster!
Controller:
Twig:
经过一番研究后我发现了一个完整的解决方案。我的解决方案假设您希望每条路线前面都有一个区域设置,甚至是登录。对此进行了修改以支持 Symfony 3,但我相信它在 2 中仍然可以工作。
此版本还假设您希望使用浏览器区域设置作为默认区域设置,如果它们转到 /admin 之类的路径,但如果它们转到 /en /admin 它会知道使用语言环境。下面的示例#2 就是这种情况。
例如:
在所有情况下,区域设置都将按照您希望在整个程序中使用的方式正确设置。
您可以查看下面的完整解决方案,其中包括如何使其与登录和安全一起使用,否则简短版本可能适合您:
完整版本
Symfony 3 将所有路由重定向到当前语言环境版本
简短版本
使其成功因此,我的示例中的情况 #2 是可能的,您需要使用 httpKernal listner
LocaleRewriteListener.php
来执行此操作。要了解其工作原理,请在 symfony 文档上查找事件订阅者界面。
要激活监听器,您需要在 services.yml 中进行设置
services.yml
最后,这指的是需要在 config.yml 中定义的变量
config.yml
最后,您需要确保所有路由现在都以 /{locale} 开头。下面是我的默认controller.php中的一个示例,
请注意要求
requirements={"_locale" = "%app.locales%"}
,这是引用config.yml文件,因此您只需在一个地方为所有路线定义这些要求。希望这对某人有帮助:)
I have a full solution to this that I discovered after some research. My solution assumes that you want every route to have a locale in front of it, even login. This is modified to support Symfony 3, but I believe it will still work in 2.
This version also assumes you want to use the browsers locale as the default locale if they go to a route like /admin, but if they go to /en/admin it will know to use en locale. This is the case for example #2 below.
So for example:
In all scenarios the locale will be set correctly how you want it for use throughout your program.
You can view the full solution below which includes how to make it work with login and security, otherwise the Short Version will probably work for you:
Full Version
Symfony 3 Redirect All Routes To Current Locale Version
Short Version
To make it so that case #2 in my examples is possible you need to do so using a httpKernal listner
LocaleRewriteListener.php
To understand how that is working look up the event subscriber interface on symfony documentation.
To activate the listner you need to set it up in your services.yml
services.yml
Finally this refers to variables that need to be defined in your config.yml
config.yml
Finally, you need to make sure all your routes start with /{locale} for now on. A sample of this is below in my default controller.php
Note the requirements
requirements={"_locale" = "%app.locales%"}
, this is referencing the config.yml file so you only have to define those requirements in one place for all routes.Hope this helps someone :)
我们创建了一个自定义 RoutingLoader,它将本地化版本添加到所有路由。您注入一组附加语言环境
['de', 'fr']
,加载器会为每个附加语言环境添加一条路由。主要优点是,对于默认区域设置,路由保持不变并且不需要重定向。另一个优点是,额外的路由被注入,因此可以针对多个客户端/环境等进行不同的配置,并且配置更少。这是加载程序:
服务定义 (SF4)
路由定义
We created a custom RoutingLoader that adds a localized version to all routes. You inject an array of additional locales
['de', 'fr']
and the Loader adds a route for each additional locale. The main advantage is, that for your default locale, the routes stay the same and no redirect is needed. Another advantage is, that the additionalRoutes are injected, so they can be configured differently for multiple clients/environments, etc. And much less configuration.Here is the loader:
Service definition (SF4)
Routing definition
我使用注释,我会做
但是对于 yml 方式,尝试一些等效的...
I use annotations, and i will do
But for yml way, try some equivalent...
也许我用一种相当简单的方式解决了这个问题:
对批评者的判断感到好奇......
最好的祝愿,
格雷格
Maybe I solved this in a reasonably simple way:
Curious about the judgement of the critics ...
Best wishes,
Greg
如何在不使用自定义控制器的情况下配置重定向到另一条路由
How to configure a redirect to another route without a custom controller
我认为您可以简单地添加这样的路由:
这样,区域设置将是用户选择的最后一个区域设置,或者如果尚未设置用户区域设置,则为默认区域设置。如果您想为 /example 设置特定的区域设置,您还可以将“_locale”参数添加到路由配置中的“默认值”中:
我不知道是否有更好的方法来做到这一点。
I think you could simply add a route like this:
This way, the locale would be the last locale selected by the user, or the default locale if user locale has not been set. You might also add the "_locale" parameter to the "defaults" in your routing config if you want to set a specific locale for /example:
I don't know if there's a better way to do this.