在区域设置路由,默认语言也没有参数 [英] Locale in Routing, Default Language also without Parameter

查看:114
本文介绍了在区域设置路由,默认语言也没有参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定义通过URL页面语言上Symfony2的安装。我的路由通过注释的工作控制器中。

I want to define the page language via url on a Symfony2 installation. My routing works via annotation inside the controller.

的routing.yml

routing.yml

_index:
    resource: "@MyMainBundle/Controller/SiteController.php"
    type:     annotation

siteController.php

siteController.php

/**
 * @Route( "/{_locale}/{site}", name="_site_site", defaults={"_locale" = "en"}, requirements={"site" = "about|cities|download|blog", "_locale" = "en|de|fr|es"} )
 */

这安静的工作很好,但是啥子我想,无非是以下网址调用同样的动作。

This works quiet well, but waht I want, is that the following url call the same action.

http://example.com/download
http://example.com/en/download
http://example.com/de/download

如果没有languge参数,页面应该切换回默认语言,但是这是我能我的行动里处理。

Without the languge-parameter, the page should switch back to the default language, but this is something I can handle inside my action.

我发现这个答案,但不能让它在所有的工作。
路由 Symfony2的默认区域设置

I found this Answer, but could not get it to work at all. Symfony2 default locale in routing

推荐答案

只需添加另一个 @Route 标注不包含的语言环境。

Just add another @Route annotation that does not include the locale.

/**
 * @Route("/{_locale}/{site}/")
 * @Route("/{site}/")
 */

这篇关于在区域设置路由,默认语言也没有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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