现在在网站中有这种情况,比如有一个 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 高级模板 多域名管理问题的更多相关文章

  1. Yii2 高级模板不使用Apache配置目录,将前后台入口移到根目录

    刚刚入手Yii2高级模板不久,部署项目时,得部署2个应用,个人感觉很繁琐,就将前后台入口文件全部拿到项目根目录.但是一看,完了,出错了!找教程找不到,还是自己解决吧 为了以后好升级,不改变Yii2核心 ...

  2. yii2高级模板使用一个域名管理前后台

    yii2的高级模板分为backend和frontend,最开始用yii的时候并没怎么在意,就使用了两个域名分别解析前后台.今天无意间看见 可以使用一个域名指向前后台. 1.修改 advanced/ba ...

  3. Yii2高级模板vendor和application非同级目录部署

    上面是Yii2的高级模板,当我们有多个application的时候,这种高级模板可以可以提供很好的扩展性,多个application共用一份YII2框架,默认情况下,框架和application是在同 ...

  4. 【备忘录】yii2高级模板多个应用启用同一个域名多个栏目

    nginx部署方式,两种写法,本人认为第一种写法没有第二种写法优雅 第一种写法配置文件: server { listen ; server_name youban-dev.jqtest.mopon.c ...

  5. Yii2高级模板的安装

    1.通过composer 安装高级版 C:wampwwwyii>composer create-project --prefer-dist yiisoft/yii2-app-advanced a ...

  6. yii2高级模板安装

    通过 Composer 安装 如果还没有安装 Composer,在 Linux 和 Mac OS X 中可以运行如下命令: curl -sS https://getcomposer.org/insta ...

  7. 对于 yii2 高级模板 生成文件入口

    安装的 advanced 模板web下是没有index.php 方法: 在advanced 目录下有个init.bat 应用程序  双击即可如下 查看advanced 目录 (刷新)如下 已有:

  8. Yii2 高级模板添加更多Application

    单独的前端和后端有时是不够的. 如果需要额外的应用程序,例如博客blog: 1.将frontend复制到blog,环境/ dev / frontend到environments / dev / blo ...

  9. yii2高级版账号密码问题

    yii2高级版默认后台没有密码,生成账号密码步骤: 1. CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` ...

随机推荐

  1. c++ [wrong]simple "Garbage Collector"

    In fact, Ptr alone can accomplish the task mentioned below. Implementation see Ptr.h, main2.cpp. In ...

  2. jquery中Uncaught TypeError: $(...).ajaxUpload is not a function(…)错误解决方法

    错误原因:该函数不是jquery的核心函数,所以需要外部引入ajaxfileupload.js文件,可能是没有引入,或者引入的js文件互相冲突 解决方法:每次进入一个函数之前打印该函数所有的js文件, ...

  3. iOS开发之删除过期Provisioning Profiles方法

    1.在finder下打开go -> go to folder输入: ~/Library/MobileDevice/Provisioning Profiles 2.查看上面的列表,依照时间顺序删除 ...

  4. 【藏】使用Entity Framework时要注意的一些性能问题

    这篇文章写的很好: http://diaosbook.com/Post/2012/12/9/performance-issue-in-select-one-or-few-colums-via-enti ...

  5. mysql 索引优化,索引建立原则和不走索引的原因

    第一:选择唯一性索引 唯一性索引的值是唯一的,可以更快捷的通过该索引来确定某条记录. 2.索引的列为where 后面经常作为条件的字段建立索引 如果某个字段经常作为查询条件,而且又有较少的重复列或者是 ...

  6. 【转】Android自动化测试(UiAutomator)——UiObject

    本文主要讲解使用UiAutomator的一些技巧,希望对于初学者有一定的帮助 UiObject 1.首先要声明对象 UiObject XXX = new UiObject(new Selector) ...

  7. asp.net 关于字符串内范围截取的一点方法总结

    前两天有一位网友提出了一个字符串内截取字符串的问题,除了用普通的字符串截取的方式外,我推荐的是用LINQ方式来截取.两者实际上差别不是很大,都是采用字符串截取方式,但后者从写法和观察效果会比前者简单实 ...

  8. windows 32位以及64位的inline hook

    Tips : 这篇文章的主题是x86及x64 windows系统下的inline hook实现部分. 32位inline hook 对于系统API的hook,windows 系统为了达成hotpatc ...

  9. Map的两张遍历方法 keySet(),entrySet()

    源博客 http://blog.csdn.net/liu826710/article/details/9001254 在Map集合中 values():方法是获取集合中的所有的值----没有键,没有对 ...

  10. mysql 集群的一些概念

    读写分离: 主备机有 master-master方式,mysql自己提供两个机器之间的备份 binlog方式,一个机器master 用于写数据,一个用于读数据,写数据的那个机器也应有读读功能,有既有读 ...