• symfon2 配置文件使用 + HttpRequest使用 + Global多语言解决方案


    1. 在 app/conig中建立一个自命名的文件: abc.yml

    2. 在 app/config/config.yml中导入abc.yml

    文件头部:

    imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: abc.yml }

    3. 在abc.yml中定义变量:

    parameters:
    myname: wangyingxi

    4. 在代码中便可以使用了:

    某action中:

    $key = $this->container->getParameter('myname');
    return new Response($key);

    app.php是程序入口!

    $request = Request::createFromGlobals()

    $request=$this->getRequest();

    $host = $request->getHost();

    获取传参:

    GET的参数:$request->query->get('aparam')

    POST的参数:$request->request->get('bar', 'default value if bar does not exist');

    HttpFundation里面的内容具体查看:

    http://symfony.com/doc/current/book/http_fundamentals.html

    多语言版本实现思路:

    1. 在Symfony的route.yml配置

    gy_mall_t1:
    pattern: /t1
    host: www.a.com
    defaults: { _controller: GyMallBundle:Default:t1, _locale: global }

    gy_mall_t2:
    pattern: /t1
    host: www.a.cn
    defaults: { _controller: GyMallBundle:Default:t1, _locale: cn }

    - 分类域名

    - 分别传参(locale值)

    2. Action部分可以接受到参数

    public function t1Action() {

      echo 'current locale is : ' . $this->get('translator')->getLocale();

      // $this->get('translator')->setLocale('fr');

      exit;

    }

    3. 在/web/.htaccess中添加:

    RewriteCond %{HTTP_HOST} ^a.com
    RewriteRule ^(.*)$ http://www.a.com/$1 [R=301,L]
    RewriteCond %{HTTP_HOST} ^a.cn
    RewriteRule ^(.*)$ http://www.a.cn/$1 [R=301,L]

    如果修改了.htaccess却无效,可以清空浏览器cookie再试试

    清空cache,重启apache2

    http://segmentfault.com/q/1010000000212748

    http://stackoverflow.com/questions/11412476/how-to-translate-language-in-symfony-2-according-to-accept-language-header

    http://symfony.com/doc/current/book/translation.html#book-translation-locale-url

    (暂无用到下面这篇文章)

    http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html

  • 相关阅读:
    弃用 btoa 转用 blob吧
    【软件调试】第二章 进程和线程实验记录
    kafka查看数据_Kafka 数据积压情况查看
    Content type 'multipart/formdata;boundary= ;charset=UTF8' not supported",
    AGC057E RowCol/ColRow Sort 【观察,组合计数】
    html基本认识+文字文本属性+盒子模型+子代选择器
    浮动+页面居中+弹性盒子+定位+图片插入
    微信小程序(1)
    一个调度系统的表面研究
    新手写flask常见的坑
  • 原文地址:https://www.cnblogs.com/vincedotnet/p/3588295.html
Copyright © 2020-2023  润新知