从Symfony2中的路由访问locale变量

时间:2015-08-03 11:38:57

标签: php symfony routing yaml

我遇到了以下问题。我正在开发一个项目,其中路由将取决于locale变量。目前正在运作,但它有一些我想解决的问题。

我得到的结构就是这个:

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;

到目前为止工作但问题是从文件中读取而不是从会话中读取。我想知道如何从会话中注入该值

1 个答案:

答案 0 :(得分:0)

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

根据我的理解,您希望仅为给定的区域设置提供一些路径。例如,法国用户将能够访问路线/ bonjour,但英国绅士将只有相同的路线/你好。

有点奇怪。为什么不使所有路由可用并更改区域设置以匹配路由?例如,如果他试图访问/ hello url,我们的法国人将有一个英文页面。看起来更简单,更方便。

相关问题