Commit e5a87af2 by Qiang Xue

Fixes #4331: `yii\helpers\Url` now uses `UrlManager` to determine base URL when generating URLs

parent 4717571e
...@@ -178,6 +178,7 @@ Yii Framework 2 Change Log ...@@ -178,6 +178,7 @@ Yii Framework 2 Change Log
- Chg #4201: change default value of `SyslogTarget::facility` from LOG_SYSLOG to LOG_USER (dizews) - Chg #4201: change default value of `SyslogTarget::facility` from LOG_SYSLOG to LOG_USER (dizews)
- Chg #4227: `\yii\widgets\LinkPager::$hideOnSinglePage` is now `true` by default (samdark) - Chg #4227: `\yii\widgets\LinkPager::$hideOnSinglePage` is now `true` by default (samdark)
- Chg #4318: `yii\helpers\Html::ul()` and `ol()` will return an empty list tag if an empty item array is given (qiangxue) - Chg #4318: `yii\helpers\Html::ul()` and `ol()` will return an empty list tag if an empty item array is given (qiangxue)
- Chg #4331: `yii\helpers\Url` now uses `UrlManager` to determine base URL when generating URLs (qiangxue)
- Chg: Replaced `clearAll()` and `clearAllAssignments()` in `yii\rbac\ManagerInterface` with `removeAll()`, `removeAllRoles()`, `removeAllPermissions()`, `removeAllRules()` and `removeAllAssignments()` (qiangxue) - Chg: Replaced `clearAll()` and `clearAllAssignments()` in `yii\rbac\ManagerInterface` with `removeAll()`, `removeAllRoles()`, `removeAllPermissions()`, `removeAllRules()` and `removeAllAssignments()` (qiangxue)
- Chg: Added `$user` as the first parameter of `yii\rbac\Rule::execute()` (qiangxue) - Chg: Added `$user` as the first parameter of `yii\rbac\Rule::execute()` (qiangxue)
- Chg: `yii\grid\DataColumn::getDataCellValue()` visibility is now `public` to allow accessing the value from a GridView directly (cebe) - Chg: `yii\grid\DataColumn::getDataCellValue()` visibility is now `public` to allow accessing the value from a GridView directly (cebe)
......
...@@ -196,13 +196,13 @@ class BaseUrl ...@@ -196,13 +196,13 @@ class BaseUrl
$hasScheme = ($pos = strpos($url, ':')) > 0 && ctype_alpha(substr($url, 0, $pos)); $hasScheme = ($pos = strpos($url, ':')) > 0 && ctype_alpha(substr($url, 0, $pos));
$char = $url[0]; $char = $url[0];
if ($char !== '/' && $char !== '#' && $char !== '.' && !$hasScheme) { if ($char !== '/' && $char !== '#' && $char !== '.' && !$hasScheme) {
$url = Yii::$app->getRequest()->getBaseUrl() . '/' . $url; $url = Yii::$app->getUrlManager()->getBaseUrl() . '/' . $url;
} }
} }
if ($scheme) { if ($scheme) {
if (empty($hasScheme)) { if (empty($hasScheme)) {
$url = Yii::$app->getRequest()->getHostInfo() . '/' . ltrim($url, '/'); $url = Yii::$app->getUrlManager()->getHostInfo() . '/' . ltrim($url, '/');
} }
if (is_string($scheme) && ($pos = strpos($url, ':')) !== false) { if (is_string($scheme) && ($pos = strpos($url, ':')) !== false) {
$url = $scheme . substr($url, $pos); $url = $scheme . substr($url, $pos);
...@@ -223,9 +223,9 @@ class BaseUrl ...@@ -223,9 +223,9 @@ class BaseUrl
*/ */
public static function base($scheme = false) public static function base($scheme = false)
{ {
$url = Yii::$app->getRequest()->getBaseUrl(); $url = Yii::$app->getUrlManager()->getBaseUrl();
if ($scheme) { if ($scheme) {
$url = Yii::$app->getRequest()->getHostInfo() . $url; $url = Yii::$app->getUrlManager()->getHostInfo() . $url;
if (is_string($scheme) && ($pos = strpos($url, '://')) !== false) { if (is_string($scheme) && ($pos = strpos($url, '://')) !== false) {
$url = $scheme . substr($url, $pos); $url = $scheme . substr($url, $pos);
} }
...@@ -306,7 +306,7 @@ class BaseUrl ...@@ -306,7 +306,7 @@ class BaseUrl
$url = Yii::$app->getHomeUrl(); $url = Yii::$app->getHomeUrl();
if ($scheme) { if ($scheme) {
$url = Yii::$app->getRequest()->getHostInfo() . $url; $url = Yii::$app->getUrlManager()->getHostInfo() . $url;
if (is_string($scheme) && ($pos = strpos($url, '://')) !== false) { if (is_string($scheme) && ($pos = strpos($url, '://')) !== false) {
$url = $scheme . substr($url, $pos); $url = $scheme . substr($url, $pos);
} }
......
...@@ -125,6 +125,7 @@ class UrlManager extends Component ...@@ -125,6 +125,7 @@ class UrlManager extends Component
public $ruleConfig = ['class' => 'yii\web\UrlRule']; public $ruleConfig = ['class' => 'yii\web\UrlRule'];
private $_baseUrl; private $_baseUrl;
private $_scriptUrl;
private $_hostInfo; private $_hostInfo;
/** /**
...@@ -298,7 +299,8 @@ class UrlManager extends Component ...@@ -298,7 +299,8 @@ class UrlManager extends Component
$route = trim($params[0], '/'); $route = trim($params[0], '/');
unset($params[0]); unset($params[0]);
$baseUrl = $this->getBaseUrl();
$baseUrl = $this->showScriptName || !$this->enablePrettyUrl ? $this->getScriptUrl() : $this->getBaseUrl();
if ($this->enablePrettyUrl) { if ($this->enablePrettyUrl) {
/* @var $rule UrlRule */ /* @var $rule UrlRule */
...@@ -364,18 +366,18 @@ class UrlManager extends Component ...@@ -364,18 +366,18 @@ class UrlManager extends Component
} }
/** /**
* Returns the base URL that is used by [[createUrl()]] to prepend URLs it creates. * Returns the base URL that is used by [[createUrl()]] to prepend to the URLs it creates.
* It defaults to [[Request::scriptUrl]] if [[showScriptName]] is true or [[enablePrettyUrl]] is false; * It defaults to [[Request::baseUrl]].
* otherwise, it defaults to [[Request::baseUrl]]. * This is mainly used when [[enablePrettyUrl]] is true and [[showScriptName]] is false.
* @return string the base URL that is used by [[createUrl()]] to prepend URLs it creates. * @return string the base URL that is used by [[createUrl()]] to prepend to the URLs it creates.
* @throws InvalidConfigException if running in console application and [[baseUrl]] is not configured. * @throws InvalidConfigException if running in console application and [[baseUrl]] is not configured.
*/ */
public function getBaseUrl() public function getBaseUrl()
{ {
if ($this->_baseUrl === null) { if ($this->_baseUrl === null) {
$request = Yii::$app->getRequest(); $request = Yii::$app->getRequest();
if ($request instanceof \yii\web\Request) { if ($request instanceof Request) {
$this->_baseUrl = $this->showScriptName || !$this->enablePrettyUrl ? $request->getScriptUrl() : $request->getBaseUrl(); $this->_baseUrl = $request->getBaseUrl();
} else { } else {
throw new InvalidConfigException('Please configure UrlManager::baseUrl correctly as you are running a console application.'); throw new InvalidConfigException('Please configure UrlManager::baseUrl correctly as you are running a console application.');
} }
...@@ -385,7 +387,8 @@ class UrlManager extends Component ...@@ -385,7 +387,8 @@ class UrlManager extends Component
} }
/** /**
* Sets the base URL that is used by [[createUrl()]] to prepend URLs it creates. * Sets the base URL that is used by [[createUrl()]] to prepend to the URLs it creates.
* This is mainly used when [[enablePrettyUrl]] is true and [[showScriptName]] is false.
* @param string $value the base URL that is used by [[createUrl()]] to prepend URLs it creates. * @param string $value the base URL that is used by [[createUrl()]] to prepend URLs it creates.
*/ */
public function setBaseUrl($value) public function setBaseUrl($value)
...@@ -394,6 +397,37 @@ class UrlManager extends Component ...@@ -394,6 +397,37 @@ class UrlManager extends Component
} }
/** /**
* Returns the entry script URL that is used by [[createUrl()]] to prepend to the URLs it creates.
* It defaults to [[Request::scriptUrl]].
* This is mainly used when [[enablePrettyUrl]] is false or [[showScriptName]] is true.
* @return string the entry script URL that is used by [[createUrl()]] to prepend to the URLs it creates.
* @throws InvalidConfigException if running in console application and [[scriptUrl]] is not configured.
*/
public function getScriptUrl()
{
if ($this->_scriptUrl === null) {
$request = Yii::$app->getRequest();
if ($request instanceof Request) {
$this->_scriptUrl = $request->getScriptUrl();
} else {
throw new InvalidConfigException('Please configure UrlManager::scriptUrl correctly as you are running a console application.');
}
}
return $this->_scriptUrl;
}
/**
* Sets the entry script URL that is used by [[createUrl()]] to prepend to the URLs it creates.
* This is mainly used when [[enablePrettyUrl]] is false or [[showScriptName]] is true.
* @param string $value the entry script URL that is used by [[createUrl()]] to prepend URLs it creates.
*/
public function setScriptUrl($value)
{
$this->_scriptUrl = $value;
}
/**
* Returns the host info that is used by [[createAbsoluteUrl()]] to prepend URLs it creates. * Returns the host info that is used by [[createAbsoluteUrl()]] to prepend URLs it creates.
* @return string the host info (e.g. "http://www.example.com") that is used by [[createAbsoluteUrl()]] to prepend URLs it creates. * @return string the host info (e.g. "http://www.example.com") that is used by [[createAbsoluteUrl()]] to prepend URLs it creates.
* @throws InvalidConfigException if running in console application and [[hostInfo]] is not configured. * @throws InvalidConfigException if running in console application and [[hostInfo]] is not configured.
......
...@@ -24,6 +24,12 @@ class UrlTest extends TestCase ...@@ -24,6 +24,12 @@ class UrlTest extends TestCase
'hostInfo' => 'http://example.com/', 'hostInfo' => 'http://example.com/',
'url' => '/base/index.php&r=site%2Fcurrent&id=42' 'url' => '/base/index.php&r=site%2Fcurrent&id=42'
], ],
'urlManager' => [
'class' => 'yii\web\UrlManager',
'baseUrl' => '/base',
'scriptUrl' => '/base/index.php',
'hostInfo' => 'http://example.com/',
]
], ],
], '\yii\web\Application'); ], '\yii\web\Application');
} }
......
...@@ -21,6 +21,7 @@ class UrlManagerTest extends TestCase ...@@ -21,6 +21,7 @@ class UrlManagerTest extends TestCase
// default setting with '/' as base url // default setting with '/' as base url
$manager = new UrlManager([ $manager = new UrlManager([
'baseUrl' => '/', 'baseUrl' => '/',
'scriptUrl' => '',
'cache' => null, 'cache' => null,
]); ]);
$url = $manager->createUrl(['post/view']); $url = $manager->createUrl(['post/view']);
...@@ -31,6 +32,7 @@ class UrlManagerTest extends TestCase ...@@ -31,6 +32,7 @@ class UrlManagerTest extends TestCase
// default setting with '/test/' as base url // default setting with '/test/' as base url
$manager = new UrlManager([ $manager = new UrlManager([
'baseUrl' => '/test/', 'baseUrl' => '/test/',
'scriptUrl' => '/test',
'cache' => null, 'cache' => null,
]); ]);
$url = $manager->createUrl(['post/view', 'id' => 1, 'title' => 'sample post']); $url = $manager->createUrl(['post/view', 'id' => 1, 'title' => 'sample post']);
...@@ -40,6 +42,7 @@ class UrlManagerTest extends TestCase ...@@ -40,6 +42,7 @@ class UrlManagerTest extends TestCase
$manager = new UrlManager([ $manager = new UrlManager([
'enablePrettyUrl' => true, 'enablePrettyUrl' => true,
'baseUrl' => '/', 'baseUrl' => '/',
'scriptUrl' => '',
'cache' => null, 'cache' => null,
]); ]);
$url = $manager->createUrl(['post/view', 'id' => 1, 'title' => 'sample post']); $url = $manager->createUrl(['post/view', 'id' => 1, 'title' => 'sample post']);
...@@ -47,13 +50,15 @@ class UrlManagerTest extends TestCase ...@@ -47,13 +50,15 @@ class UrlManagerTest extends TestCase
$manager = new UrlManager([ $manager = new UrlManager([
'enablePrettyUrl' => true, 'enablePrettyUrl' => true,
'baseUrl' => '/test/', 'baseUrl' => '/test/',
'scriptUrl' => '/test',
'cache' => null, 'cache' => null,
]); ]);
$url = $manager->createUrl(['post/view', 'id' => 1, 'title' => 'sample post']); $url = $manager->createUrl(['post/view', 'id' => 1, 'title' => 'sample post']);
$this->assertEquals('/test/post/view?id=1&title=sample+post', $url); $this->assertEquals('/test/post/view?id=1&title=sample+post', $url);
$manager = new UrlManager([ $manager = new UrlManager([
'enablePrettyUrl' => true, 'enablePrettyUrl' => true,
'baseUrl' => '/test/index.php', 'baseUrl' => '/test',
'scriptUrl' => '/test/index.php',
'cache' => null, 'cache' => null,
]); ]);
$url = $manager->createUrl(['post/view', 'id' => 1, 'title' => 'sample post']); $url = $manager->createUrl(['post/view', 'id' => 1, 'title' => 'sample post']);
...@@ -72,6 +77,7 @@ class UrlManagerTest extends TestCase ...@@ -72,6 +77,7 @@ class UrlManagerTest extends TestCase
], ],
], ],
'baseUrl' => '/', 'baseUrl' => '/',
'scriptUrl' => '',
]); ]);
$url = $manager->createUrl(['post/view', 'id' => 1, 'title' => 'sample post']); $url = $manager->createUrl(['post/view', 'id' => 1, 'title' => 'sample post']);
$this->assertEquals('/post/1/sample+post', $url); $this->assertEquals('/post/1/sample+post', $url);
...@@ -89,6 +95,7 @@ class UrlManagerTest extends TestCase ...@@ -89,6 +95,7 @@ class UrlManagerTest extends TestCase
], ],
], ],
'baseUrl' => '/', 'baseUrl' => '/',
'scriptUrl' => '',
'suffix' => '.html', 'suffix' => '.html',
]); ]);
$url = $manager->createUrl(['post/view', 'id' => 1, 'title' => 'sample post']); $url = $manager->createUrl(['post/view', 'id' => 1, 'title' => 'sample post']);
...@@ -108,6 +115,7 @@ class UrlManagerTest extends TestCase ...@@ -108,6 +115,7 @@ class UrlManagerTest extends TestCase
], ],
], ],
'baseUrl' => '/test', 'baseUrl' => '/test',
'scriptUrl' => '/test',
]); ]);
$url = $manager->createUrl(['post/view', 'id' => 1, 'title' => 'sample post', 'lang' => 'en']); $url = $manager->createUrl(['post/view', 'id' => 1, 'title' => 'sample post', 'lang' => 'en']);
$this->assertEquals('http://en.example.com/test/post/1/sample+post', $url); $this->assertEquals('http://en.example.com/test/post/1/sample+post', $url);
...@@ -119,6 +127,7 @@ class UrlManagerTest extends TestCase ...@@ -119,6 +127,7 @@ class UrlManagerTest extends TestCase
{ {
$manager = new UrlManager([ $manager = new UrlManager([
'baseUrl' => '/', 'baseUrl' => '/',
'scriptUrl' => '',
'hostInfo' => 'http://www.example.com', 'hostInfo' => 'http://www.example.com',
'cache' => null, 'cache' => null,
]); ]);
......
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