yii url 具有 2 个参数的管理器

yii url manager with 2 params

我想创建一个这样的 url: http://domain.com/index.php/hir/2015-02-12/news_title

这是我的 url 经理不工作的地方:

 'urlManager'=>array(
    'urlFormat'=>'path',
    'rules'=>array(
        'hir/<date:\d+>/<title:\w+>'=>'news/view',
        '<controller:\w+>/<id:\d+>'=>'<controller>/view',
        '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
        '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
    ),
  ),

这是我的 .htaccess:

Options +FollowSymLinks
IndexIgnore */*
<IfModule mod_rewrite.c>
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
</IfModule>
RewriteEngine on

不需要.htaccess文件,可以通过url-manager解决。只需像这样更改您的第一个 url-manager 规则:

'hir/<date:\d+>/<title:\w+>'=>'news/view' TO 'hir/<date>/<title>'=>'news/view'

我想这会对你有所帮助。

您的 URL 模式几乎是正确的。

但是 <date:\d+> 不允许您的日期格式 (yyyy-mm-d)。因为你给了 Regex \d,它是 [0-9] 的 shorthand。它不允许 hyphen sign 在你的日期字符串中。

所以,更改正则表达式,它可以允许您的日期格式。

我可以建议:在您的 urlManager 中使用 <date:[\w-]+> 而不是 <date:\d+>