【问题标题】:Yii 1.1 create custom user friendly URLsYii 1.1 创建自定义用户友好的 URL
【发布时间】:2015-12-30 15:51:50
【问题描述】:

我正在尝试制定自定义 URL 要求。我们是一个基于产品的网站,每个产品的 url 就像

https://www.domainname.com/product/index/id/product_id

现在我希望网址是用户友好的,类似这样..

https://www.domainname.com/productcustomurl

为了实现这一点,我在表product 中创建了一个名为custom_url 的字段,其中customurl 将由产品创建者在产品创建期间存储。为此,我在 params.php 中创建了一个规则,如下所示:

'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'<id:(.*?)>' => 'product/index' //this rule

假设每当有人在参数id 中使用productcustomurl 命中https://www.domainname.com/productcustomurl 时,这将重定向用户,这工作正常,但问题是这会将所有内容重定向到product/index,覆盖所有其他路由,例如site/indexsite/about 等等等等,这是一个问题。我希望他们工作。如何在不影响网站正常路由的情况下实现?? 请帮忙。

【问题讨论】:

    标签: php yii url-rewriting url-routing params


    【解决方案1】:

    尝试用这个替换你的最后一行配置:

    '<productcustomurl:\w>' => 'product/index' 
    

    现在,actionIndexProductController 应该是这样的:

    public function actionIndex($productcustomurl)
    {
         //Now, you can find a product by its custom url, and pass the finded product object to the view
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 2015-06-28
      • 2015-06-02
      • 2011-01-06
      • 1970-01-01
      • 2011-11-26
      相关资源
      最近更新 更多