Yii2 Url使用多个字符串参数进行规则

时间:2017-01-28 12:05:03

标签: php regex yii2

我有两个字符串参数设置url规则的问题。我有动作"描述"在控制器"文章"像这样:

public function actionDescription($cat, $subcat){ ... }

然后我使用不同的方法设置网址规则,如下所示:

'Article/description/<cat:\S+>/<subcat:\S+>' => 'article/description'
'Article/description/<cat:\S+>/<subcat>' => 'article/description'
'Article/description/<cat:\S+>/<subcat:.*?>' => 'article/description'

我仍然得到这个网址(第二个参数与规则不匹配):

http://test.com/article/description/category+of+article?subcat=subcategory+of+article

Instaed of

http://test.com/article/description/category+of+article/subcategory+of+article

我这样称呼网址:

<?=  Html::a( Html::encode($subcategory->scName), ['article/description', 
                       'cat' =>$categoryModel->ccCategory, 
                       'subcat' =>$subcategory->scName], 
                        ['class' => 'subcat_link'])         
?>

1 个答案:

答案 0 :(得分:0)

尝试此规则

'article/description/<cat:[\w\s]+>/<subcat:[\w\s]+>' => 'article/description'

使用此示例

echo Url::to(['/article/description', 'cat' => 'category', 'subcat' => 'subcategory']);