mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-14 13:25:23 +08:00
Fixes #11309: Added yii\web\Request::getHostName() method that returns hostname of current request
This commit is contained in:
committed by
Alexander Makarov
parent
e53cc5e938
commit
63f95fa3ad
@@ -57,7 +57,8 @@ Yii Framework 2 Change Log
|
||||
- Enh #10583: Do not silence session errors in debug mode (samdark)
|
||||
- Enh #11096: Added support for PSR-2 style migration naming for namespaced migrations (klimov-paul)
|
||||
- Enh #11245: Added `yii\rbac\ManagerInterface::getChildRoles()` method, allowing finding child roles for the given one (githubjeka)
|
||||
- Enh #11275: Added possibility of unset or force replace former value in `ArrayHelper::merge()` (mdmunir, rob006)
|
||||
- Enh #11275: Added possibility of unset or force replace former value in `yii\helpers\ArrayHelper::merge()` (mdmunir, rob006)
|
||||
- Enh #11309: Added `yii\web\Request::getHostName()` method that returns hostname of current request (rob006)
|
||||
- Enh #11494: `yii.reloadableScripts` now support wildcards with `*` character (silverfire)
|
||||
- Enh #11658: Added argument to `yii\grid\ActionColumn::urlCreator` callback, which holds reference to the column instance (klimov-paul)
|
||||
- Enh #11804: Added `yii\behaviors\AttributeTypecastBehavior` for maintaining of strict ActiveRecord attribute types (klimov-paul)
|
||||
|
||||
@@ -521,14 +521,15 @@ class Request extends \yii\base\Request
|
||||
}
|
||||
|
||||
private $_hostInfo;
|
||||
private $_hostName;
|
||||
|
||||
/**
|
||||
* Returns the schema and host part of the current request URL.
|
||||
* The returned URL does not have an ending slash.
|
||||
* By default this is determined based on the user request information.
|
||||
* You may explicitly specify it by setting the [[setHostInfo()|hostInfo]] property.
|
||||
* @return string schema and hostname part (with port number if needed) of the request URL (e.g. `http://www.yiiframework.com`),
|
||||
* null if can't be obtained from `$_SERVER` and wasn't set.
|
||||
* @return string|null schema and hostname part (with port number if needed) of the request URL
|
||||
* (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set.
|
||||
* @see setHostInfo()
|
||||
*/
|
||||
public function getHostInfo()
|
||||
@@ -554,13 +555,30 @@ class Request extends \yii\base\Request
|
||||
* Sets the schema and host part of the application URL.
|
||||
* This setter is provided in case the schema and hostname cannot be determined
|
||||
* on certain Web servers.
|
||||
* @param string $value the schema and host part of the application URL. The trailing slashes will be removed.
|
||||
* @param string|null $value the schema and host part of the application URL. The trailing slashes will be removed.
|
||||
*/
|
||||
public function setHostInfo($value)
|
||||
{
|
||||
$this->_hostName = null;
|
||||
$this->_hostInfo = $value === null ? null : rtrim($value, '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the host part of the current request URL.
|
||||
* Value is calculated from current [[getHostInfo()|hostInfo]] property.
|
||||
* @return string|null hostname part of the request URL (e.g. `www.yiiframework.com`)
|
||||
* @see getHostInfo()
|
||||
* @since 2.0.10
|
||||
*/
|
||||
public function getHostName()
|
||||
{
|
||||
if ($this->_hostName === null) {
|
||||
$this->_hostName = parse_url($this->getHostInfo(), PHP_URL_HOST);
|
||||
}
|
||||
|
||||
return $this->_hostName;
|
||||
}
|
||||
|
||||
private $_baseUrl;
|
||||
|
||||
/**
|
||||
|
||||
@@ -245,10 +245,12 @@ class RequestTest extends TestCase
|
||||
$request = new Request();
|
||||
|
||||
unset($_SERVER['SERVER_NAME'], $_SERVER['HTTP_HOST']);
|
||||
$this->assertEquals(null, $request->getHostInfo());
|
||||
$this->assertSame(null, $request->getHostInfo());
|
||||
$this->assertSame(null, $request->getHostName());
|
||||
|
||||
$request->setHostInfo('http://servername.com:80');
|
||||
$this->assertEquals('http://servername.com:80', $request->getHostInfo());
|
||||
$this->assertSame('http://servername.com:80', $request->getHostInfo());
|
||||
$this->assertSame('servername.com', $request->getHostName());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user