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
最新文章
- High Precision Timers in iOS / OS X
- 创建数据库指定路径sql
- Python面向对象1
- Posix IPC
- WordPress Backdoor未授权访问漏洞和信息泄露漏洞
- Centos6.4 为用户添加sudo功能
- java类的初始化
- golang中container/heap包源码分析
- Sqlmap Tamper大全(1)
- 剑指offer数组3
- Python的自动补全
- POST 上传 JSON 数据
- Latex citation using natbib and footnotesize
- ORACLE SQL Developer日期显示格式不全
- Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换(转载)
- google-gson 使用及GsonBuilder设置
- tomcat源码阅读之过滤器
- Sqlserver查询数据库文件大小和剩余空间
- 几种常见的Web服务器
- 转载----开发运维资产管理系统cmdb一些观点扯淡