You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.4KB

  1. <?php
  2. namespace common\components;
  3. use Yii;
  4. use yii\helpers\Url;
  5. use yii\base\InvalidConfigException;
  6. use yii\web\UrlManager ;
  7. class UrlManagerCommon extends UrlManager
  8. {
  9. public $subDomain;
  10. public $domainName;
  11. protected $_hostInfo;
  12. public function getProperDomain()
  13. {
  14. if ( ! isset($this->domainName) || empty($this->domainName) ) {
  15. throw new InvalidConfigException('Request requires a domain name to be configured!');
  16. }
  17. $subDomain = (isset($this->subDomain) && !empty($this->subDomain)) ? $this->subDomain : '';
  18. $domain = empty($subDomain) ? '' : $subDomain . '.';
  19. $domain .= $this->domainName;
  20. return $domain;
  21. }
  22. public function getHostInfo()
  23. {
  24. if ($this->_hostInfo === null)
  25. {
  26. $secure = Yii::$app->getRequest()->getIsSecureConnection();
  27. $http = $secure ? 'https' : 'http';
  28. if (isset($_SERVER['HTTP_HOST'])) {
  29. $this->_hostInfo = $http . '://' . $this->getProperDomain();
  30. } elseif (isset($_SERVER['SERVER_NAME'])) {
  31. $this->_hostInfo = $http . '://' . $this->getProperDomain();
  32. $port = $secure ? $this->getSecurePort() : $this->getPort();
  33. if (($port !== 80 && !$secure) || ($port !== 443 && $secure)) {
  34. $this->_hostInfo .= ':' . $port;
  35. }
  36. }
  37. }
  38. return $this->_hostInfo;
  39. }
  40. }