urlManager规则与自定义url


urlManager rules with custom urls

我希望能够访问/创建这样的URL:

http://mywebsite.com/player/Name-Surname-100/

在视图中,我的链接是使用以下内容正确生成的:

$this->createUrl('player/view', array('id' => $player->id, 'slug' => $player->slug))

在我的main.php配置文件中,我定义了以下规则:

'player/<slug:'w+>-<id:'d+>' => 'ui/player/view',

但没有成功(我收到以下错误消息:无法解决请求"player/Name-Surname-100"。)。在我的"玩家"模型(表)中,我还有"鼻涕虫"属性(列)。

编辑:

http://mywebsite.com/player/Name-Surname-anotherName-100/ //not working
http://mywebsite.com/player/Name_Surname_anotherName-100/ //it works!!

您的urlManager规则将"-"作为slug和id之间的分隔符。

使用Larry的urlManager规则允许URL中的"-":

'player/<slug:['w'-]+>-<id:'d+>' => 'ui/player/view'

另一种方法是替换段塞中的所有"-"。

$this->createUrl('player/view', array('id' => $player->id, 'slug' => str_replace('-','_'$player->slug));