【问题标题】:access to locale variable from routing in Symfony2从 Symfony2 中的路由访问语言环境变量
【发布时间】:2015-08-03 11:38:57
【问题描述】:

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

我得到的结构是这样的:

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;

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

【问题讨论】:

标签: php symfony routing yaml


【解决方案1】:

我又一次被自己缺乏声誉所困扰,不能只对你的帖子发表评论。

据我了解,您希望某些路线仅适用于给定的语言环境。例如,法国用户将能够访问路径 /bonjour,但英国绅士将只能访问等效路径 /hello。

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

【讨论】:

    猜你喜欢
    • 2011-08-13
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 2013-07-10
    • 1970-01-01
    • 2021-09-19
    相关资源
    最近更新 更多