标签:
现在在网站中有这种情况,比如有一个 http://frontend.com/tv 需要根据判断用户的 User Agent ,如果用户是手机浏览器的话,则跳转到 http://mobile.com/tv。
还有就是需要反过来的情况,比如用户在 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
标签:
原文地址:http://www.cnblogs.com/yhdsir/p/5181906.html