dongshi1914 2015-08-03 11:38
浏览 36

从Symfony2中的路由访问locale变量

I got the following issue. I am developing a project in which the routes will depend upon the locale variable. Currently is working but it has some issues that I would like to fix.

The structure I got is this one:

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 }

The php file is invoked and this is what it does

//@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;

It so far works but the problem is that is reading from the file and not from the session. I would like to know how can I inject that value from the session

  • 写回答

1条回答 默认 最新

  • douzuan5365 2015-08-03 13:04
    关注

    Once again i am plagued with my lack of reputation and cannot just leave a comment on your post.

    From what I understand, you want to make some routes available only for a given locale. For example, a french user will be able to access the route /bonjour but an english gentleman will only have the equivalent route /hello.

    It is a bit weird. Why not make all routes available and change the locale to match the route? For example our french fellow will have a page in english if he tries to access the /hello url. Seems a lot more simple and handy.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测