[英]access to locale variable from routing in 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;
到目前为止,它仍然有效,但问题是从文件而不是从会话读取。 我想知道如何从会话中注入该值
我再一次因缺乏声誉而困扰,不能只对您的帖子发表评论。
据我了解,您希望使某些路由仅适用于给定的语言环境。 例如,法国用户将可以访问/ bonjour路线,而英国绅士将仅具有等效的/ hello路线。
这有点奇怪。 为什么不让所有路由可用并更改语言环境以匹配该路由? 例如,如果我们的法国同胞尝试访问/ hello网址,则会有一个英文页面。 似乎更简单方便。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.