yii2conditional-statementsyii-url-manager

Is there any way to trigger urlManager conditionally in YII framework


I have following code-

'urlManager'=>array(
        'urlFormat'=>'path',
                    //'showScriptName'=>false,
                    //'caseSensitive'=>false,
        'rules'=>array(
            ''=>'site/home',
            'logout'=>'site/logout',
            'login' =>'site/login',
            'page/about' => 'page/show/id/1',               
            '<cat0:[a-zA-Z\-0-9]+>/<cat1:[a-zA-Z\-0-9]+>/<name:[a-zA-Z\-0-9]+>' => 'product/single',
            '<cat0:[a-zA-Z\-0-9]+>/<cat1:[a-zA-Z\-0-9]+>' => 'product/products',
            '<cat0:[a-zA-Z\-0-9 ]+>' => 'product/subcategories',

        ),
    ),

but when I used any site path example-

/site/contact/

Yii consider it as cat0 and cat1 hence it forwards me towards the product/products controller with $_GET['cat0'] = site and $_GET['cat1'] = contact.

Is there any way out for it?


Solution

  • This is wrong. Your rules say:

    any_word/any_word will go to product/products

    i.e. site/contact will go to product/products

    So try this: products/<cat0:[a-zA-Z\-0-9]+>/<cat1:[a-zA-Z\-0-9]+>' => 'product/products',

    Now products/cat1/cat2 will go to /product/products

    For more read this section: Adding Rules Dynamically