2015-08-03 46 views
0

我遇到了以下问题。我正在开发一个项目,其中路线将取决于区域设置变量。目前正在工作,但它有一些我想解决的问题。在Symfony2中从路由访问语言环境变量

我得到的结构是这样的一个:

AppBundle 
->config 
    ->routing.en.yml #routes in English 
    ->routing.es.yml #routes in Spanish 
    ->routing.php #in charge of making the magic 

#/app/config/routing.yml 
app: 
    resource: "@AppBundle/config/routing.php" 
    type: php 

fos_user: 
    resource: "@FOSUserBundle/Resources/config/routing/all.xml" 

#@AppBundle/config/routing.es.yml 
about_us: 
    path: /nosotros 
    defaults: { _controller: AppBundle:Default:about } 

#@AppBundle/config/routing.en.yml 
about_us: 
    path: /about_us 
    defaults: { _controller: AppBundle:Default:about } 

php文件被调用,这是它做什么

//@AppBundle/config/routing.php 
<?php 
use Symfony\Component\Routing\RouteCollection; 
use Symfony\Component\Routing\Route; 
use Symfony\Component\Yaml\Yaml; 

$collection = new RouteCollection(); 
$collection->addCollection(
    $loader->import("@AppBundle/Controller/", "annotation") 
); 

$config = Yaml::parse(__DIR__."/../../../app/config/parameters.yml"); 
$locale = $config["parameters"]["locale"]; 

$routes = Yaml::parse(__DIR__."/routing.{$locale}.yml"); 
foreach($routes as $name => $data) 
{ 
    $collection->add($name, new Route($data["path"],$data["defaults"])); 
} 

return $collection; 

它迄今为止的作品,但问题是,从阅读文件,而不是来自会话。我想知道我该如何注入会议的价值

+0

您的问题已经在这里找到答案: [http://stackoverflow.com/questions/19484027/translations-in-symfony-2-3-locale-in-request][1] [1]:http://stackoverflow.com/questions/19484027/translations-in-symfony-2-3-locale-in-request –

回答

0

我再次受到我缺乏声誉的困扰,不能只对你的帖子发表评论。

从我所了解的情况来看,您希望只为给定的语言环境提供一些路由。例如,法国用户将能够访问路线/ bonjour,但英国绅士只能拥有相同的路线/ hello。

这有点奇怪。为什么不使所有路线都可用并更改语言环境以匹配路线?例如,如果他尝试访问/ hello url,我们的法国人将会有英文页面。看起来更简单和方便。