2015-08-03 141 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,我們的法國人將會有英文頁面。看起來更簡單和方便。