Search code examples
phpregexyiiyii1.x

Yii URL Manger create a route with multi optional parameters


I have a URL rule with multiple optional parameters and it was working but it stopped after I upgraded from Yii 1.1.15 to 1.1.19.

const OPTIONAL_PARAMS = '(/<featured:featured>)?'
. '(/subType/<subType:.*?>)?'
. '(/type/<type:\d+>)?'
. '(/category/<category:.*>)?';


'<lang:(en|fr)>/reports'. OPTIONAL_PARAMS => 'reports',

Any one can provide me some ideas or someone faced similar issues?

P.S. The other URL manager rules are working fine, only this one with (SOME_CODE)? for optional params is not working. I'm using PHP 5.6 and Apache.


Solution

  • You may be interested in by this issue. But in short: this syntax (regexp outside of named params) was never officially supported and it was removed as a bugfix in Yii 1.1.17.

    The last version which supports this is 1.1.16, but it is really old and I would not recommend using it. You should probably create custom UrlRule and use it instead of CUrlRule for this particular case.


    You may also try to add /* at the end of the pattern, like this:

    '<lang:(en|fr)>/reports/*' => 'reports',
    

    This will allow to append GET params to URL as /key/value. So this:

    $this->createUrl('reports', [
        'type' => 'sometype', 
        'category' => 'somecategory',
        'lang' => 'en',
    ]);
    

    will create URL like:

    /en/reports/type/sometype/category/somecategory