kirSeNN kirSeNN - 1 year ago 245
PHP Question

yii2 URL Management two named sub-patterns

I have module structure in my Yii 2.0 application, so I need to call things like


But 90% of modules and controllers have the same names, for example it looks like


It looks not good, I'd like to see just "profile/update"... Then I create the following rule in UrlManager:

"<controller:\w+>/<action:\w+>" => "<controller>/<controller>/<action>"

This gets me the error: "preg_match(): Compilation failed: two named subpatterns have the same name at offset 35"

How to solve this problem instead of creating different rules for each module, I don't want my config file has several rules for tens modules.

Answer Source

I found better solution. PCRE supports modifier "?J" that allows duplicate names for subpatterns. All we need is to add this modifier to our pattern that Yii will use for parsing url. Unfortunately we have to add it manually, in yii/web/UrlRule change line:

$this->_routeRule = '#^' . strtr($this->route, $tr2) . '$#u';


$this->_routeRule = '#^(?J)' . strtr($this->route, $tr2) . '$#u';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download