0

我想获取如下 URL:

http://domain.com/post/1/some-titles-here

但我得到:

http://domain.com/post/1?title=some-titles-here

我正在使用这个配置:

'urlFormat' => 'path',
...
//'post/<id:\d+>/<title>' => 'post/view/', 
'post/<id:\d+>/<title:\w+>' => 'post/view/',
'post/<id:\d+>' => 'post/view/',
...

然后获取我正在执行的 URL:

Yii::app()->createAbsoluteUrl('post/view', array('id' => $this->id,'title' => $this->title));

我在这里遵循第三条规则: http ://www.yiiframework.com/doc/guide/1.1/en/topics.url#using-named-parameters

4

2 回答 2

1

The regular expression you're using to match the title is incorrect: <title:\w+> will only match single words but your title has hyphens as well.

Tuan is correct; it's matching the next rule. That's because the URL manager works its way down the rules until it finds one that matches.

Use this rule instead:

'post/<id:\d+>/<title:([A-Za-z0-9-]+)>' => 'post/view/',

That will match titles with letters, numbers, and hyphens.

于 2013-04-30T21:04:57.493 回答
0

这是对我有用的代码:

'urlManager'=>array(
    'urlFormat'=>'path',
    'showScriptName' => false,
    'rules'=>array(
         '' => 'site/index',
         'article/<id:\d+>/<alias:[-\w]+>' => 'article/view',
         'article/cat/<id:\d+>-<alias:[-\w]+>' => 'category/view',

结果网址:

于 2014-12-27T01:41:16.477 回答