对于多语言网站,我们可以用 en.fecshop.com,fr.fecshop.com,es.fecshop.com, 这种子域名的方式表现做多语言

也可以用 www.fecshop.com , www.fecshop.com/fr , www.fecshop.com/es , 这种方式做多语言

yii2对第一种支持还是不错,对于第二种支持不好,我们搞一种自己的方式来支持这种。

1.在app/web/下面新建文件夹

app/web/fr

app/web/es

在上面的文件夹下面新建index.php文件,assets文件夹设置可写。

/app/web/fr/index.php

1.nginx设置:

在nginx的server配置中加入:

location /fr/ {

index index.php;

if (!-e $request_filename){

rewrite . /fr/index.php last;

}

}

2.入口/app/web/fr/index.php文件开始加入:

$secure = 0;

$http = $secure ? 'https' : 'http';

$homeUrl = $http.'://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['SCRIPT_NAME']), '\\/');

$application = new yii\web\Application($config);

的上面加入:

$config['homeUrl'] = $homeUrl;

加完之后的样子:

//$secure = isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') === 0;

$secure = 0;

$http = $secure ? 'https' : 'http';

$homeUrl = $http.'://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['SCRIPT_NAME']), '\\/');

defined('YII_DEBUG') or define('YII_DEBUG', true);

defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/../../../vendor/autoload.php');

require(__DIR__ . '/../../../vendor/yiisoft/yii2/Yii.php');

require(__DIR__ . '/../../../common/config/bootstrap.php');

require(__DIR__ . '/../../config/bootstrap.php');

$config = yii\helpers\ArrayHelper::merge(

require(__DIR__ . '/../../../common/config/main.php'),

require(__DIR__ . '/../../../common/config/main-local.php'),

require(__DIR__ . '/../../config/main.php'),

require(__DIR__ . '/../../config/main-local.php')

);

$config['homeUrl'] = $homeUrl;

$application = new yii\web\Application($config);

$application->run();

3.添加store组件:

'store' => [

'class' => 'fecshop\services\Store',

'stores' => [

'fecshop.appadmin.fancyecommerce.com/fr' => [

'language' => 'fr',

'themePackage'=> 'default',

'theme'=> 'default',

'currency' => 'USD',

],

'fecshop.appadmin.fancyecommerce.com/es' => [

'language' => 'es',

'themePackage'=> 'default',

'theme'=> 'default',

'currency' => 'USD',

],

'fecshop.appadmin.fancyecommerce.com' => [

'language'  => 'en',

'themePackage' => 'default',

'theme' => 'default',

'currency' => 'USD',

],

],

'languages' => [

//'en','fr','it','de','es','nl','pt','ru',

],

],

组件:

/**

* FecShop file.

*

* @link http://www.fecshop.com/

* @copyright Copyright (c) 2016 FecShop Software LLC

* @license http://www.fecshop.com/license/

*/

namespace fecshop\services;

use Yii;

use yii\base\InvalidValueException;

use yii\base\InvalidConfigException;

use yii\base\BootstrapInterface;

/**

* @author Terry Zhao <2358269014@qq.com>

* @since 1.0

*/

class Store extends Service implements BootstrapInterface

{

/**

* init by config file.

* all stores config . include : domain,language,theme,themePackage

*/

public $stores;

/**

* init by config file.

* all store Language.

*/

public $languages;

/**

* current store language

*/

public $currentLanguage = 'en';

/**

* current store theme package

*/

public $currentThemePackage = 'default';

/**

* current store theme

*/

public $currentTheme = 'default';

/**

* current store name , this property will init value with domain.

*/

public $currentStore;

/**

*Bootstrap:init website, class property $currentLanguage ,$currentTheme and $currentStore.

* if you not config this ,default class property will be set.

* if current domain is not config , InvalidValueException will be throw.

*class property $currentStore will be set value $domain.

*/

public function bootstrap($app){

$host = explode('://' ,$app->getHomeUrl());

$stores = $this->stores;

$init_compelte = 0;

if(is_array($stores) && !empty($stores)){

foreach($stores as $domain => $lang){

if($host[1] == $domain){

Yii::$app->store->currentStore = $domain;

if(isset($lang['language']) && !empty($lang['language'])){

Yii::$app->store->currentLanguage = $lang['language'];

}

if(isset($lang['theme']) && !empty($lang['theme'])){

Yii::$app->store->currentTheme = $lang['theme'];

}

if(isset($lang['themePackage']) && !empty($lang['themePackage'])){

Yii::$app->store->currentThemePackage = $lang['themePackage'];

}

/**

* init store currency.

*/

if(isset($lang['currency']) && !empty($lang['currency'])){

$currency = $lang['currency'];

}else{

$currency = '';

}

Yii::$app->page->currency->initCurrency($currency);

/**

* current domian is config is store config.

*/

$init_compelte = 1;

}

}

}

if(!$init_compelte){

throw new InvalidValueException('this domain is not config in store component');

}

}

/**

* if a object or array attribute is a store attribute, you can get current

* language value by this function.

*/

public function getLangVal($attr,$attrName){

return $attr[$this->currentLanguage."_".$attrName];

}

public function getAllLanguage(){

}

}

上面通过相应的域名设置不同的语言,货币,模板等。

4.yii的yii\helpers\Url已经对我们不适合,我们需要自己写一个url的生成:

namespace fec\helpers;

use Yii;

class CUrl

{

public static $_baseHttpUrl;

public static $_baseHttpsUrl;

# 1.获取首页地址。

public static function getHomeUrl(){

return Yii::$app->getHomeUrl();

//return Yii::$app->getBaseUrl(true);

}

# 2. 获取首页地址。同上

public static function getBaseUrl($isHttps=false){

if($isHttps){

if(!self::$_baseHttpsUrl){

self::$_baseHttpsUrl = str_replace('http','https',self::getHomeUrl());

}

return self::$_baseHttpsUrl;

}else{

if(!self::$_baseHttpUrl){

self::$_baseHttpUrl = str_replace('https','http',self::getHomeUrl());

}

return self::$_baseHttpUrl;

}

}

# 3.立即跳转 和 yii2的跳转还是不同

public static function redirect($url,$isHttps=false){

if($url){

if(substr($url,0,4) != "http"){

$url = self::getUrl($url,[],$isHttps);

}

header("Location: $url");

exit;

}

}

# 4.通过模板name,得到对应文件路径。

# 默认是 domain.com/skin/theme/下面的绝对URL

public static function getSkinUrl($dir = '',$relative_path=false){

$currentTheme = CConfig::getCurrentTheme();

$url = '';

if(!$relative_path){

$url = self::getHomeUrl(). DIRECTORY_SEPARATOR;

}

return $url.'skin'.DIRECTORY_SEPARATOR

.$currentTheme.DIRECTORY_SEPARATOR

.$dir;

}

#5. 通过url path 和参数 得到当前网站下的完整url路径。

public static function getUrl($url_path,$params=array(),$isHttps=false){

$url_path = trim($url_path,DIRECTORY_SEPARATOR);

$url = self::getBaseUrl($isHttps). DIRECTORY_SEPARATOR .$url_path;

$str = "";

if(!empty($params) && is_array($params)){

$str .= "?";

foreach($params as $k=>$v){

$str .= $k."=".$v."&";

}

$str = substr($str,0,strlen($str)-1);

}

return $url.$str;

}

# 6.得到当前的完整url

public static function getCurrentUrl(){

//$s = self::getHomeUrl();

//return $s.$_SERVER["REQUEST_URI"];

return \yii\helpers\Url::current();

}

# 7.得到当前的完整url no param

public static function getCurrentUrlNoParam(){

$url = self::getCurrentUrl();

if(strstr($url,"#")){

$url = substr($url,0,strpos($url,"#"));

}

if(strstr($url,"?")){

$url = substr($url,0,strpos($url,"?"));

}

return $url;

}

# 8、得到url key ,譬如 http://www.x.com/ss/dd/aa?aaaa=ddddd 返回 /ss/dd/aa

public static function getUrlKey(){

return Yii::$app->request->getPathInfo();

}

# 9.得到url ,譬如 http://www.x.com/ss/dd/aa?aaaa=ddddd 返回 /ss/dd/aa?aaaa=ddddd

public static function getUrlKeyWithParam(){

return Yii::$app->getRequest()->url;

}

}

得到url:

CUrl::getUrl('/x/x/x',['p'=>'2]);

#将生成

http://fecshop.appadmin.fancyecommerce.com/fr/x/x/x?p=2

CUrl::getUrl('/x/x/x',['p'=>'2],rue);

#将生成

https://fecshop.appadmin.fancyecommerce.com/fr/x/x/x?p=2

OK,到这里就完成整个过程了。

yii 获取当前域名_yii2 在域名后面加一个路径作为首页相关推荐

  1. Yii获取当前url和域名

    如果我们当前页面的訪问地址是:http://localhost/CMS/public/index.php? r=news&id=1 一. 1.获取当前域名:echoYii::app()-> ...

  2. 获取顶级域名与一级域名的python库-tld

    可以参考:https://pypi.org/project/tld/ 获得顶级域名与一级域名的python库.get_tld与get_fld from tld import get_tld url = ...

  3. 爬虫工具获取页面中域名及子域名(SQL注入、渗透)

    目录 0x01 获取页面中的 URL 0x02 提取 URL 中带参数的 URL 0x03 将提取出来的 URL 去重 总结 自动化寻找网站的注入漏洞,需要先将目标网站的所有带参数的 URL 提取出来 ...

  4. 计算机中DW用户名和域名,如何获取线程关联用户名和域名

    如何获取线程关联用户名和域名 作者/ 在Windows NT/2000/XP上编写程序时,有时会需要我们获取与当前调用线程关联的用户名和域名(domain),本文下面将示范在Windows NT/20 ...

  5. natapp自动获取免费的动态端口域名

    前段时间,因为客户有个项目要求跨局域网进行远程控制桌面,想知道能不能实现.于是查询了许多资料,了解到需要有公网服务器作为中介才能够实现,但是公司又没有公网服务器,于是只有利用花生壳.natapp服务器 ...

  6. 获取CloudFlare上的所有域名的ID (zone_identifier) - by PHP

    效果图 CF_api_get_zone_ids.php <?php/*** Title: 获取CloudFlare上的所有域名的ID (zone_identifier)* Author: Rud ...

  7. excel函数获取长域名的顶级域名

    excel函数获取长域名的顶级域名) 在空白单元格输入以下内容,G3 为需要被截取的对象单元格 =MID(G3,FIND("|",SUBSTITUTE(G3,".&quo ...

  8. PHP实现获取url地址中一级域名

    本文实例讲述了PHP实现获取url地址中顶级域名的方法.分享给大家供大家参考,具体如下:parse_url()获取到的host时多级域名,如:mp.weixin.qq.com.做域名黑名单的时候我们需 ...

  9. java获取一级域名 正则_正则获取各类URL的一级域名代码是否可行?

    有许多网址, 类似abc.abc.com ,abc.com/abc ,www.abc.com.cn , abc.com.tw ,www.abc.co.uk ,www.abc.com.jp/abc.ph ...

  10. 获取当前网页的协议+域名

    // 获取当前网页的协议+域名getPageBaseUrl () {let baseURL = ''if (!window.location.origin) { // 兼容IE,IE11版本下loca ...

最新文章

  1. mysql免安装版的问题
  2. nginx lua 小项目:根据 user_agent 显示不同的页面,附带和 php 性能的对比
  3. 字节跳动又一款中重度游戏曝光,它要进军“漫改MMO”领域!
  4. 牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结...
  5. app式成语_疯狂的成语app
  6. 1.7Oob 继承关系中构造方法的使用
  7. 后端把Long类型的数据传给前端,前端可能会出现精度丢失的情况,以及解决方案...
  8. java获取本周的开始时间和结束时间_创业板注册制开始时间/股票开户流程结束后,怎么炒股?...
  9. C++ 临时变量的常量性
  10. 解决“此图片来自微信公众平台未经允许不可引用”的方法
  11. 申请一个公网ip多少钱_申请1个条形码多少钱?小作坊如何申请条形码?
  12. day01 格式化输出和while循环的两个小练习
  13. psd缩略图上传控件
  14. urp教务系统简单利用
  15. java计算机毕业设计社区生活超市管理系统源程序+mysql+系统+lw文档+远程调试
  16. Macos下netbeans常用快捷键
  17. python小欢喜(八)俄罗斯方块 (5) 生成各种组合形状
  18. mysql取去年年初_查询年初,年末,去年年初,明年年初与年末sql语句
  19. 996 成福报?让员工二选一:996 或 11/11/6
  20. 阿里云官网全新版本抢先看

热门文章

  1. mybatis-plus 自定义UpdateWrapper(二)实现列的case set
  2. mybatis foreach标签的使用
  3. 线程安全问题和Synchronized的使用
  4. POI操作Excel详解,HSSF和XSSF两种方式
  5. 项目maven依赖成功,但编译一直报错:引用项目的类路径找不到
  6. 操作mysql5.7过程中遇到的问题
  7. Yann LeCun说是时候放弃概率论了,因果关系才是理解世界的基石
  8. 在linux本地下载ftp中的文件
  9. unity camera aspect
  10. 对Javascript异步执行的理解