I extended yii\web\UrlRule and modified [[parseRequest()]] and [[createUrl()]].
I also added 2 public properties.
In my config, i add the following:
'ruleConfig' => [
'class' => 'my\custom\UrlRule',
'propertyX' => 'abc',
'propertyY' =>'xyz',
],
When i run the my application with the following, i get the following:
Unknown Property – yii\base\UnknownPropertyException
Setting unknown property: yii\web\UrlRule::propertyX
The cause is related to yii\debug\Module and yii\gii\Module. When i disable both, it works.
| Q | A
| ---------------- | ---
| Yii version | 2.0.11.2
| PHP version | 7.0.10
Both Gii and Debug do the following at bootstrap stage:
Yii::$app->getUrlManager()->addRules([
[
'class' => 'yii\web\UrlRule',
'route' => 'x',
'pattern' => 'x',
],
], false);
And the issue is about UrlManager::buildRules():
$rule = Yii::createObject(array_merge($this->ruleConfig, $rule));
Resulting array in your case is:
'ruleConfig' => [
'class' => 'yii\web\UrlRule',
'route' => 'x',
'pattern' => 'x',
'propertyX' => 'abc',
'propertyY' =>'xyz',
],
It works as described:
Individual rule configurations specified via [[rules]] will take precedence when the same property of the rule is configured.
So we can fix that in Gii and Debug extensions.
I was reading about:
Individual rule configurations specified via [[rules]] will take precedence when the same property of the rule is configured.
In the docs as well then realised it should be in gii and debug issues.
Thanks for referencing them.
Closing the issue here since core is correct.
I get a similar problem then i changing ruleConfig in yii\web\UrlRule and set custom class for Rule still get old rules with different class from cache. UrlManager::init() calculate hash only for rules but nothing with they configuration.
if (is_string($this->cache)) {
$this->cache = Yii::$app->get($this->cache, false);
}
if ($this->cache instanceof CacheInterface) {
$cacheKey = $this->cacheKey;
$hash = md5(json_encode($this->rules));
if (($data = $this->cache->get($cacheKey)) !== false && isset($data[1]) && $data[1] === $hash) {
$this->rules = $data[0];
} else {
$this->rules = $this->buildRules($this->rules);
$this->cache->set($cacheKey, [$this->rules, $hash]);
}
} else {
$this->rules = $this->buildRules($this->rules);
}
@dezmound You should open new issue for your problem, it is completely unrelated to this issue.
Most helpful comment
Both Gii and Debug do the following at bootstrap stage: