symfon2 配置文件使用 + HttpRequest使用 + Global多语言解决方案
2014-03-08 18:04
561 查看
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
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
相关文章推荐
- linux之UDP网络编程
- 网络流题目集锦(by 戴神)
- 算法杂货铺——分类算法之贝叶斯网络(Bayesian networks)
- Android 4.0调用http接口php网站的api
- 基于PXE的网络自动安装系统
- linux tcp多线程服务器与客户端编程实例
- httpclient4 取得cookie信息并保存
- 通过httpClient4读取页面内容
- HttpClient 4 - 文件上传
- HttpClient 4 实现文件下载
- HttpClient 4 设置超时
- HttpClient4 警告: Invalid cookie header 的问题解决
- HttpClient4基础2--通过认证代理访问网页
- HttpClient4基础1--通过匿名代理访问网页
- httpClient通过代理(Http Proxy)进行请求
- Linux入门之pxe网络自动安装系统----“高级的自动化”
- 网络编程之 Socket函数 (一)
- Linux系统-小倒腾之Linux DIY定制裁剪(附带简单网络功能)o_o(一)
- CustomerHttpClient单例遇到的超时问题
- linux网络编程之socket(二):C/S程序的一般流程和基本socket函数