ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] #### 1.在config 头加上domain信息 ~~~ $host = explode('.', $_SERVER["HTTP_HOST"]); if (count($host) > 2) { define('DOMAIN', $host[1] . '.' . $host[2]); } else { define('DOMAIN', $host[0] . '.' . $host[1]); } define('DOMAIN_WWW', 'www.' . DOMAIN); define('DOMAIN_ADMIN', 'admin.' . DOMAIN); ~~~ #### 2.配置config里的urlManager ~~~ 'urlManager' => [ 'class' => 'common\components\MutilpleDomainUrlManager', 'domains' => [ 'www' => '//' . DOMAIN_WWW, 'admin' => '//' . DOMAIN_ADMIN, ], 'showScriptName' => false, 'enablePrettyUrl' => true, ], ~~~ #### 3.创建MutilpleDomainUrlManager ~~~ namespace common\components; use Yii; class MutilpleDomainUrlManager extends \yii\web\UrlManager { public $domains = array(); public function createUrl($domain, $params = array()) { if (func_num_args() === 1) { $params = $domain; $domain = false; } $bak = $this->getBaseUrl(); if ($domain) { if (!isset($this->domains[$domain])) { throw new \yii\base\InvalidConfigException('Please configure UrlManager of domain "' . $domain . '".'); } $this->setBaseUrl($this->domains[$domain]); } $url = parent::createUrl($params); $this->setBaseUrl($bak); return $url; } } ~~~ #### 4.代码测试 ~~~ Yii::$app->urlManager->createUrl('site/index'), # /site/index Yii::$app->urlManager->createUrl('admin', 'site/login'), # admin.xxx.com/site/login Yii::$app->urlManager->createUrl('www', 'user/view'), # www.xxx.com/user/view ~~~