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

时间:2022-11-04 15:58:38

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://*.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