zoukankan      html  css  js  c++  java
  • 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' => 'commoncomponentsMultipleAppUrlManager',
                '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 commoncomponents;
    
    use Yii;
    use yiiwebUrlManager;
    
    class MultipleAppUrlManager extends yiiwebUrlManager
    {
        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 yiiaseInvalidConfigException
         */
        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 yiiaseInvalidConfigException('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 yiiaseInvalidConfigException
         */
        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 yiiaseInvalidConfigException('Please configure UrlManager of apps "' . $appId . '".');
                }
                $appUrlManager = $this->_loadOtherAppInstance($appId);
    
                return $appUrlManager->createAbsoluteUrl($params);
            }
        }
    
        private $_appInstances = [];
    
        /**
         * @param string $appId
         * @return UrlManager
         * @throws yiiaseInvalidConfigException
         */
        private function _loadOtherAppInstance($appId)
        {
            if (!isset($this->_appInstances[$appId])) {
                $this->_appInstances[$appId] = Yii::createObject([
                        'class' => 'yiiwebUrlManager',
                    ] + $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

  • 相关阅读:
    Spring Boot 打包插件,真是太有用了!
    java高级应用:线程池全面解析
    漫画:HTTP 协议极简教程,傻瓜都能看懂!
    Tomcat 连接数与线程池详解
    Intellij IDEA Debug 调试技巧
    Java 程序员必须掌握的 5 个注解!
    如何优雅地终止一个线程?
    springmvc实现REST中的GET、POST、PUT和DELETE
    @Resource 和 @Autowired注解的异同
    SpringMVC的各种参数绑定方式
  • 原文地址:https://www.cnblogs.com/yhdsir/p/5181906.html
Copyright © 2011-2022 走看看