Browse Source

fixed caching issue with UrlManager::createUrl

this fix will skip the cache in case we encounter a situation which can
not easily be cached.

fixes #9091
tags/2.0.6
Carsten Brandt 9 years ago
parent
commit
854e186210
  1. 1
      framework/CHANGELOG.md
  2. 9
      framework/web/UrlManager.php
  3. 20
      tests/framework/web/UrlManagerTest.php

1
framework/CHANGELOG.md

@ -32,6 +32,7 @@ Yii Framework 2 Change Log
- Bug #9046: Fixed problem with endless error loop when an error occurred after sending a stream or file download response to the user (cebe)
- Bug #9059: Fixed PHP Notice in error handler view (dynasource, andrewnester, samdark)
- Bug #9063: Workaround for MySQL losing table case when adding index (sebathi)
- Bug #9091: `UrlManager::createUrl()` did not create correct url when defaults were used, internal cache is now skipped in certain situations (cebe)
- Bug #9127, #9128: Fixed MSSQL `QueryBuilder::renameColumn()` and `QueryBuilder::renameTable()` escaping (sitawit)
- Bug #9161: Fixed `yii\web\Request` ignore `queryParams` when resolve request (zetamen)
- Bug: Fixed string comparison in `BaseActiveRecord::unlink()` which may result in wrong comparison result for hash valued primary keys starting with `0e` (cebe)

9
framework/web/UrlManager.php

@ -325,9 +325,16 @@ class UrlManager extends Component
}
if ($url === false) {
$cacheable = true;
foreach ($this->rules as $rule) {
if (!empty($rule->defaults) && $rule->mode !== UrlRule::PARSING_ONLY) {
// if there is a rule with default values involved, the matching result may not be cached
$cacheable = false;
}
if (($url = $rule->createUrl($this, $route, $params)) !== false) {
$this->_ruleCache[$cacheKey][] = $rule;
if ($cacheable) {
$this->_ruleCache[$cacheKey][] = $rule;
}
break;
}
}

20
tests/framework/web/UrlManagerTest.php

@ -125,6 +125,26 @@ class UrlManagerTest extends TestCase
$this->assertEquals('http://en.example.com/test/post/1/sample+post', $url);
$url = $manager->createUrl(['post/index', 'page' => 1]);
$this->assertEquals('/test/post/index?page=1', $url);
// create url with the same route but different params/defaults
$manager = new UrlManager([
'enablePrettyUrl' => true,
'cache' => null,
'rules' => [
[
'pattern' => '',
'route' => 'frontend/page/view',
'defaults' => ['slug' => 'index'],
],
'page/<slug>' => 'frontend/page/view',
],
'baseUrl' => '/test',
'scriptUrl' => '/test',
]);
$url = $manager->createUrl(['frontend/page/view', 'slug' => 'services']);
$this->assertEquals('/test/page/services', $url);
$url = $manager->createUrl(['frontend/page/view', 'slug' => 'index']);
$this->assertEquals('/test/', $url);
}
/**

Loading…
Cancel
Save