Yii URL Manager:创建视图URL时出现问题

时间:2014-05-05 20:40:15

标签: yii rewrite

在文件config main.php中:

'/view-details/<slug:[a-zA-Z0-9]+>' => array('product/details', 'urlSuffix' => '.html')

创建网址:

$this->createUrl('product/details', array('slug'=>'my-product-demo-with-id-123'));

结果:

mydomain/view-details/my-product-demo-with-id-123.html

(完成!完美生成网址)

但是,当访问链接时,这是错误:未解决的请求view-details / my-product-demo-with-id-123.html

如果删除所有字符&#34; - &#34; (mydomain / view-details / MyProductDemoWithID123.html),它正在运行,而不是错误。

什么是问题?有人可以帮帮我吗?

4 个答案:

答案 0 :(得分:1)

您正在使用的正则表达式只匹配单个单词,但您的网址也有连字符。

您需要有一个看起来像这样的规则

'/view-details/<slug:[-\w]+>' => array('product/details', 'urlSuffix' => '.html')

答案 1 :(得分:0)

我不确定(Yii的新内容!),但这可能与您的Controller actionView()中的错误方法有关。 看看你的控制器中是否有#34;查看详细信息&#34;与&#34; actionMyProductDemoWithID123()&#34;类似的方法在此内部,您渲染该文件。

示例:yourDomain / yourController / yourView

Class YourController{
      actionYourView(){
        $this->render('yourView');
   }
}

在你的情况下,你可能有类似的东西:

Class view-details{
          actionMyProductDemoWithId123(){
            $this->render('MyProductDemoWithID123');
       }
    }

如果不是,你可以查看这篇文章:http://www.yiiframework.com/wiki/404/hyphenation-of-routes-in-url-management/

答案 2 :(得分:0)

您错过了正则表达式的“ - ”部分,因此无法匹配尝试

'/view-details/<slug:[a-zA-Z0-9\-]+>' => array('product/details', 'urlSuffix' => '.html')

答案 3 :(得分:0)

在正则表达式中,您只允许使用字母数字值。你必须允许&#34; - &#34;也就像你在参数(slug)中使用一样。

正则表达式使用字母数字和 - ,_

'view-details/<slug:[a-zA-Z0-9-_]+>' => array('product/details','urlSuffix' => '.html'), 
相关问题