Commit 48448864 by Carsten Brandt

added schema parameter to createAbsoluteUrl() to force 'http' or 'https'

fixes #1585
parent ab59a795
...@@ -59,6 +59,7 @@ Yii Framework 2 Change Log ...@@ -59,6 +59,7 @@ Yii Framework 2 Change Log
- Enh #1572: Added `yii\web\Controller::createAbsoluteUrl()` (samdark) - Enh #1572: Added `yii\web\Controller::createAbsoluteUrl()` (samdark)
- Enh #1579: throw exception when the given AR relation name does not match in a case sensitive manner (qiangxue) - Enh #1579: throw exception when the given AR relation name does not match in a case sensitive manner (qiangxue)
- Enh #1581: Added `ActiveQuery::joinWith()` and `ActiveQuery::innerJoinWith()` to support joining with relations (qiangxue) - Enh #1581: Added `ActiveQuery::joinWith()` and `ActiveQuery::innerJoinWith()` to support joining with relations (qiangxue)
- Enh #1585: added schema parameter to createAbsoluteUrl() to force 'http' or 'https' (cebe)
- Enh #1601: Added support for tagName and encodeLabel parameters in ButtonDropdown (omnilight) - Enh #1601: Added support for tagName and encodeLabel parameters in ButtonDropdown (omnilight)
- Enh #1611: Added `BaseActiveRecord::markAttributeDirty()` (qiangxue) - Enh #1611: Added `BaseActiveRecord::markAttributeDirty()` (qiangxue)
- Enh #1633: Advanced application template now works with MongoDB by default (samdark) - Enh #1633: Advanced application template now works with MongoDB by default (samdark)
......
...@@ -167,12 +167,14 @@ class Controller extends \yii\base\Controller ...@@ -167,12 +167,14 @@ class Controller extends \yii\base\Controller
* *
* @param string $route the route. This can be either an absolute route or a relative route. * @param string $route the route. This can be either an absolute route or a relative route.
* @param array $params the parameters (name-value pairs) to be included in the generated URL * @param array $params the parameters (name-value pairs) to be included in the generated URL
* @param string $schema the schema to use for the url. e.g. 'http' or 'https'. If not specified
* the schema of the current request will be used.
* @return string the created absolute URL * @return string the created absolute URL
*/ */
public function createAbsoluteUrl($route, $params = []) public function createAbsoluteUrl($route, $params = [], $schema = null)
{ {
$route = $this->getNormalizedRoute($route); $route = $this->getNormalizedRoute($route);
return Yii::$app->getUrlManager()->createAbsoluteUrl($route, $params); return Yii::$app->getUrlManager()->createAbsoluteUrl($route, $params, $schema);
} }
/** /**
......
...@@ -277,17 +277,21 @@ class UrlManager extends Component ...@@ -277,17 +277,21 @@ class UrlManager extends Component
* This method prepends the URL created by [[createUrl()]] with the [[hostInfo]]. * This method prepends the URL created by [[createUrl()]] with the [[hostInfo]].
* @param string $route the route * @param string $route the route
* @param array $params the parameters (name-value pairs) * @param array $params the parameters (name-value pairs)
* @param string $schema the schema to use for the url. e.g. 'http' or 'https'. If not specified
* the schema of the current request will be used.
* @return string the created URL * @return string the created URL
* @see createUrl() * @see createUrl()
*/ */
public function createAbsoluteUrl($route, $params = []) public function createAbsoluteUrl($route, $params = [], $schema = null)
{ {
$url = $this->createUrl($route, $params); $url = $this->createUrl($route, $params);
if (strpos($url, '://') !== false) { if (strpos($url, '://') === false) {
return $url; $url = $this->getHostInfo($schema) . $url;
} else { }
return $this->getHostInfo() . $url; if ($schema !== null && ($pos = strpos($url, '://')) !== false) {
$url = $schema . substr($url, $pos);
} }
return $url;
} }
/** /**
......
...@@ -124,6 +124,13 @@ class UrlManagerTest extends TestCase ...@@ -124,6 +124,13 @@ class UrlManagerTest extends TestCase
]); ]);
$url = $manager->createAbsoluteUrl('post/view', ['id' => 1, 'title' => 'sample post']); $url = $manager->createAbsoluteUrl('post/view', ['id' => 1, 'title' => 'sample post']);
$this->assertEquals('http://www.example.com?r=post/view&id=1&title=sample+post', $url); $this->assertEquals('http://www.example.com?r=post/view&id=1&title=sample+post', $url);
$url = $manager->createAbsoluteUrl('post/view', ['id' => 1, 'title' => 'sample post'], 'https');
$this->assertEquals('https://www.example.com?r=post/view&id=1&title=sample+post', $url);
$manager->hostInfo = 'https://www.example.com';
$url = $manager->createAbsoluteUrl('post/view', ['id' => 1, 'title' => 'sample post'], 'http');
$this->assertEquals('http://www.example.com?r=post/view&id=1&title=sample+post', $url);
} }
public function testParseRequest() public function testParseRequest()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment