Commit 79274688 by Carsten Brandt

urlManager: ensure route is trimmed by / on empty pattern

fixes #6717
parent 528389f0
...@@ -10,6 +10,7 @@ Yii Framework 2 Change Log ...@@ -10,6 +10,7 @@ Yii Framework 2 Change Log
- Bug #6632: `yii\di\Container::get()` did not handle config parameter correctly when it is passed as a constructor parameter (qiangxue) - Bug #6632: `yii\di\Container::get()` did not handle config parameter correctly when it is passed as a constructor parameter (qiangxue)
- Bug #6648: Added explicit type casting to avoid dblib issues on SQL Server 2014 (o-rey) - Bug #6648: Added explicit type casting to avoid dblib issues on SQL Server 2014 (o-rey)
- Bug #6691: Fixed console help description parsing with UTF8 characters (cebe) - Bug #6691: Fixed console help description parsing with UTF8 characters (cebe)
- Bug #6717: Fixed issue with UrlManager not matching a route on url creation when it was prefixed with `/` and pattern was empty (cebe)
- Enh #4502: Added alias support to URL route when calling `Url::toRoute()` and `Url::to()` (qiangxue, lynicidn) - Enh #4502: Added alias support to URL route when calling `Url::toRoute()` and `Url::to()` (qiangxue, lynicidn)
- Enh #6247: Logger and error handler are now using slightly less memory (stepanselyuk, samdark) - Enh #6247: Logger and error handler are now using slightly less memory (stepanselyuk, samdark)
- Enh #6434: Added `yii\behaviors\SluggableBehavior::immutable` to support keeping the generated slug unchanged (trntv) - Enh #6434: Added `yii\behaviors\SluggableBehavior::immutable` to support keeping the generated slug unchanged (trntv)
......
...@@ -199,6 +199,7 @@ class UrlManager extends Component ...@@ -199,6 +199,7 @@ class UrlManager extends Component
$rule = ['route' => $rule]; $rule = ['route' => $rule];
if (preg_match("/^((?:($verbs),)*($verbs))\\s+(.*)$/", $key, $matches)) { if (preg_match("/^((?:($verbs),)*($verbs))\\s+(.*)$/", $key, $matches)) {
$rule['verb'] = explode(',', $matches[1]); $rule['verb'] = explode(',', $matches[1]);
// rules that do not apply for GET requests should not be use to create urls
if (!in_array('GET', $rule['verb'])) { if (!in_array('GET', $rule['verb'])) {
$rule['mode'] = UrlRule::PARSING_ONLY; $rule['mode'] = UrlRule::PARSING_ONLY;
} }
......
...@@ -131,6 +131,7 @@ class UrlRule extends Object implements UrlRuleInterface ...@@ -131,6 +131,7 @@ class UrlRule extends Object implements UrlRuleInterface
} }
$this->pattern = trim($this->pattern, '/'); $this->pattern = trim($this->pattern, '/');
$this->route = trim($this->route, '/');
if ($this->host !== null) { if ($this->host !== null) {
$this->host = rtrim($this->host, '/'); $this->host = rtrim($this->host, '/');
...@@ -150,7 +151,6 @@ class UrlRule extends Object implements UrlRuleInterface ...@@ -150,7 +151,6 @@ class UrlRule extends Object implements UrlRuleInterface
$this->pattern = '/' . $this->pattern . '/'; $this->pattern = '/' . $this->pattern . '/';
} }
$this->route = trim($this->route, '/');
if (strpos($this->route, '<') !== false && preg_match_all('/<(\w+)>/', $this->route, $matches)) { if (strpos($this->route, '<') !== false && preg_match_all('/<(\w+)>/', $this->route, $matches)) {
foreach ($matches[1] as $name) { foreach ($matches[1] as $name) {
$this->_routeParams[$name] = "<$name>"; $this->_routeParams[$name] = "<$name>";
......
...@@ -123,6 +123,48 @@ class UrlManagerTest extends TestCase ...@@ -123,6 +123,48 @@ class UrlManagerTest extends TestCase
$this->assertEquals('/test/post/index?page=1', $url); $this->assertEquals('/test/post/index?page=1', $url);
} }
/**
* https://github.com/yiisoft/yii2/issues/6717
*/
public function testCreateUrlWithEmptyPattern()
{
$manager = new UrlManager([
'enablePrettyUrl' => true,
'cache' => null,
'rules' => [
'' => 'front/site/index',
],
'baseUrl' => '/',
'scriptUrl' => '',
]);
$url = $manager->createUrl(['front/site/index']);
$this->assertEquals('/', $url);
$url = $manager->createUrl(['/front/site/index']);
$this->assertEquals('/', $url);
$url = $manager->createUrl(['front/site/index', 'page' => 1]);
$this->assertEquals('/?page=1', $url);
$url = $manager->createUrl(['/front/site/index', 'page' => 1]);
$this->assertEquals('/?page=1', $url);
$manager = new UrlManager([
'enablePrettyUrl' => true,
'cache' => null,
'rules' => [
'' => '/front/site/index',
],
'baseUrl' => '/',
'scriptUrl' => '',
]);
$url = $manager->createUrl(['front/site/index']);
$this->assertEquals('/', $url);
$url = $manager->createUrl(['/front/site/index']);
$this->assertEquals('/', $url);
$url = $manager->createUrl(['front/site/index', 'page' => 1]);
$this->assertEquals('/?page=1', $url);
$url = $manager->createUrl(['/front/site/index', 'page' => 1]);
$this->assertEquals('/?page=1', $url);
}
public function testCreateAbsoluteUrl() public function testCreateAbsoluteUrl()
{ {
$manager = new UrlManager([ $manager = new UrlManager([
......
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