Fixes #11309: Added yii\web\Request::getHostName() method that returns hostname of current request

This commit is contained in:
Robert Korulczyk
2016-10-07 00:00:14 +02:00
committed by Alexander Makarov
parent e53cc5e938
commit 63f95fa3ad
3 changed files with 30 additions and 9 deletions

View File

@@ -57,7 +57,8 @@ Yii Framework 2 Change Log
- Enh #10583: Do not silence session errors in debug mode (samdark) - 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 #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 #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 #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 #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) - Enh #11804: Added `yii\behaviors\AttributeTypecastBehavior` for maintaining of strict ActiveRecord attribute types (klimov-paul)

View File

@@ -231,15 +231,15 @@ class Request extends \yii\base\Request
if (isset($_POST[$this->methodParam])) { if (isset($_POST[$this->methodParam])) {
return strtoupper($_POST[$this->methodParam]); return strtoupper($_POST[$this->methodParam]);
} }
if (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) { if (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
return strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']); return strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
} }
if (isset($_SERVER['REQUEST_METHOD'])) { if (isset($_SERVER['REQUEST_METHOD'])) {
return strtoupper($_SERVER['REQUEST_METHOD']); return strtoupper($_SERVER['REQUEST_METHOD']);
} }
return 'GET'; return 'GET';
} }
@@ -521,14 +521,15 @@ class Request extends \yii\base\Request
} }
private $_hostInfo; private $_hostInfo;
private $_hostName;
/** /**
* Returns the schema and host part of the current request URL. * Returns the schema and host part of the current request URL.
* The returned URL does not have an ending slash. * The returned URL does not have an ending slash.
* By default this is determined based on the user request information. * By default this is determined based on the user request information.
* You may explicitly specify it by setting the [[setHostInfo()|hostInfo]] property. * 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`), * @return string|null schema and hostname part (with port number if needed) of the request URL
* null if can't be obtained from `$_SERVER` and wasn't set. * (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set.
* @see setHostInfo() * @see setHostInfo()
*/ */
public function getHostInfo() public function getHostInfo()
@@ -554,13 +555,30 @@ class Request extends \yii\base\Request
* Sets the schema and host part of the application URL. * Sets the schema and host part of the application URL.
* This setter is provided in case the schema and hostname cannot be determined * This setter is provided in case the schema and hostname cannot be determined
* on certain Web servers. * 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) public function setHostInfo($value)
{ {
$this->_hostName = null;
$this->_hostInfo = $value === null ? null : rtrim($value, '/'); $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; private $_baseUrl;
/** /**

View File

@@ -245,10 +245,12 @@ class RequestTest extends TestCase
$request = new Request(); $request = new Request();
unset($_SERVER['SERVER_NAME'], $_SERVER['HTTP_HOST']); 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'); $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());
} }
/** /**