Yii2 高级模板 多域名管理问题
现在在网站中有这种情况,比如有一个 http://frontend.com/tv 需要根据判断用户的 User Agent ,如果用户是手机浏览器的话,则跳转到 http://mobile.com/tv。
- frontend.com 所对应 frontend 应用
- mobile.com 对应 mobile 应用
还有就是需要反过来的情况,比如用户在 PC 上访问 http://mobile.com/tv ,需要能自动跳到 http://frontend.com/tv
对于这种多域名的操作的话,大家是怎么处理的?
我这边现在是这样子的,建立了一个 MultipleAppUrlManager
的组件
这个组件配置方式如下:
return [
'components' => [
'urlManager' => [
'class' => 'common\components\MultipleAppUrlManager',
'apps' => [
'app-mobile' => [
'hostInfo' => 'http://mobile.com',
'baseUrl' => '',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
'frontend' => [
'hostInfo' => 'http://frontend.com',
'baseUrl' => '',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
],
],
],
];
组件源码:
<?php namespace common\components; use Yii;
use yii\web\UrlManager; class MultipleAppUrlManager extends \yii\web\UrlManager
{
public $apps = []; public function init()
{
if (isset($this->apps[Yii::$app->id])) {
$currentAppConfig = $this->apps[Yii::$app->id];
foreach ($currentAppConfig as $attribute => $value) {
$this->$attribute = $value;
}
} parent::init();
} /**
* @param array $params
* @param null $appId
* @return string
* @throws \yii\base\InvalidConfigException
*/
public function createUrl($params = [], $appId = null)
{
if ($appId === null || $appId === Yii::$app->id) {
return parent::createUrl($params);
} else {
if (!isset($this->apps[$appId])) {
throw new \yii\base\InvalidConfigException('Please configure UrlManager of apps "' . $appId . '".');
}
$appUrlManager = $this->_loadOtherAppInstance($appId); return $appUrlManager->createUrl($params);
}
} /**
* @param array|string $params
* @param null $scheme
* @param null $appId
* @return string
* @throws \yii\base\InvalidConfigException
*/
public function createAbsoluteUrl($params, $scheme = null, $appId = null)
{
if ($appId === null || $appId === Yii::$app->id) {
return parent::createAbsoluteUrl($params, $scheme);
} else {
if (!isset($this->apps[$appId])) {
throw new \yii\base\InvalidConfigException('Please configure UrlManager of apps "' . $appId . '".');
}
$appUrlManager = $this->_loadOtherAppInstance($appId); return $appUrlManager->createAbsoluteUrl($params);
}
} private $_appInstances = []; /**
* @param string $appId
* @return UrlManager
* @throws \yii\base\InvalidConfigException
*/
private function _loadOtherAppInstance($appId)
{
if (!isset($this->_appInstances[$appId])) {
$this->_appInstances[$appId] = Yii::createObject([
'class' => '\yii\web\UrlManager',
] + $this->apps[$appId]);
} return $this->_appInstances[$appId];
} public function getHostInfo($appId = null)
{
if ($appId === null || $appId === Yii::$app->id) {
return parent::getHostInfo();
} else {
$appUrlManager = $this->_loadOtherAppInstance($appId); return $appUrlManager->getHostInfo();
}
}
}
现在如果要跳转的话是这样写的:
# mobile tv absolute url
return Yii::$app->getUrlManager()->createAbsoluteUrl('tv', null, 'app-mobile'); # frontend tv absolute url
return Yii::$app->getUrlManager()->createAbsoluteUrl('tv', null, 'frontend');
来源:http://www.getyii.com/topic/214
Yii2 高级模板 多域名管理问题的更多相关文章
- Yii2 高级模板不使用Apache配置目录,将前后台入口移到根目录
刚刚入手Yii2高级模板不久,部署项目时,得部署2个应用,个人感觉很繁琐,就将前后台入口文件全部拿到项目根目录.但是一看,完了,出错了!找教程找不到,还是自己解决吧 为了以后好升级,不改变Yii2核心 ...
- yii2高级模板使用一个域名管理前后台
yii2的高级模板分为backend和frontend,最开始用yii的时候并没怎么在意,就使用了两个域名分别解析前后台.今天无意间看见 可以使用一个域名指向前后台. 1.修改 advanced/ba ...
- Yii2高级模板vendor和application非同级目录部署
上面是Yii2的高级模板,当我们有多个application的时候,这种高级模板可以可以提供很好的扩展性,多个application共用一份YII2框架,默认情况下,框架和application是在同 ...
- 【备忘录】yii2高级模板多个应用启用同一个域名多个栏目
nginx部署方式,两种写法,本人认为第一种写法没有第二种写法优雅 第一种写法配置文件: server { listen ; server_name youban-dev.jqtest.mopon.c ...
- Yii2高级模板的安装
1.通过composer 安装高级版 C:wampwwwyii>composer create-project --prefer-dist yiisoft/yii2-app-advanced a ...
- yii2高级模板安装
通过 Composer 安装 如果还没有安装 Composer,在 Linux 和 Mac OS X 中可以运行如下命令: curl -sS https://getcomposer.org/insta ...
- 对于 yii2 高级模板 生成文件入口
安装的 advanced 模板web下是没有index.php 方法: 在advanced 目录下有个init.bat 应用程序 双击即可如下 查看advanced 目录 (刷新)如下 已有:
- Yii2 高级模板添加更多Application
单独的前端和后端有时是不够的. 如果需要额外的应用程序,例如博客blog: 1.将frontend复制到blog,环境/ dev / frontend到environments / dev / blo ...
- yii2高级版账号密码问题
yii2高级版默认后台没有密码,生成账号密码步骤: 1. CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` ...
随机推荐
- 一个奇怪的EL表达式错误
下图是在Struts2的action中写的一个方法 JSP页面代码如下: 在页面访问如下路径:http://localhost:8088/maven_ssh/cust_getCustList 目前推测 ...
- Python os._exit() sys.exit() exit()区别
Python退出程序的方式有两种:os._exit(), sys.exit() 1)os._exit() 直接退出 Python程序,其后的代码也不会继续执行. 2)sys.exit() 引发一个 S ...
- java -- 路径中包含空格怎么处理
@.使用toURI()方法 String rootPath = this.getClass().getClassLoader().getResource(".").toURI(). ...
- fedora上安装sun jdk
系统被来就有openjdk,但是开发工具需要sun的jdk,于是下载一个压缩包并解压到一个位置.使用alternative命令切换 alternatives --.0_79/jre/bin/java ...
- linux grep sed awk
$ grep ‘test’ d* 显示所有以d开头的文件中包含 test的行. $ grep ‘test’ aa bb cc 显示在aa,bb,cc文件中匹配test的行. $ grep ‘[a-z] ...
- python 爬虫1 Urllib库的基本使用
1.简单使用 import urllib2 response = urllib2.urlopen("http://www.baidu.com") print response.re ...
- VS2005 Manifest 配置问题总结
一.问题 编译某个遗留工程后,运行程序时报错,“由于应用程序的配置不正确,应用程序无法启动.重新安装应用程序可能会解决这个问题.” 查看生成的Manifest文件如下: <?xml versio ...
- iframe详解
如何查看是否为iframe *使用FireFox组件firebug->firepath 1.Top Window:可直接定位 2.iframe#i:根据id定位 定位方法: switch_to. ...
- Linux常用命令及Vim使用
1.ls 命令 --------------------------------------------------------------------- ls以默认方式显示当前目录文件列表 ls - ...
- LIS(模板)
记录一下,O(nlgn)的算法求LIS //HHH #include <iostream> #include <stdio.h> #include <string.h&g ...