From 63f95fa3adc73fc479514cfdc856cd25f75d4cec Mon Sep 17 00:00:00 2001 From: Robert Korulczyk Date: Fri, 7 Oct 2016 00:00:14 +0200 Subject: [PATCH] Fixes #11309: Added `yii\web\Request::getHostName()` method that returns hostname of current request --- framework/CHANGELOG.md | 3 ++- framework/web/Request.php | 30 +++++++++++++++++++++++------ tests/framework/web/RequestTest.php | 6 ++++-- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index ff983c6c1d..6af716e4db 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -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) diff --git a/framework/web/Request.php b/framework/web/Request.php index 4631e2058d..05b61f96f8 100644 --- a/framework/web/Request.php +++ b/framework/web/Request.php @@ -231,15 +231,15 @@ class Request extends \yii\base\Request if (isset($_POST[$this->methodParam])) { return strtoupper($_POST[$this->methodParam]); } - + if (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) { return strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']); } - + if (isset($_SERVER['REQUEST_METHOD'])) { return strtoupper($_SERVER['REQUEST_METHOD']); } - + return 'GET'; } @@ -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; /** diff --git a/tests/framework/web/RequestTest.php b/tests/framework/web/RequestTest.php index b5a59b6eba..b9d5c49176 100644 --- a/tests/framework/web/RequestTest.php +++ b/tests/framework/web/RequestTest.php @@ -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()); } /**