在Yii2中形成URL

时间:2015-04-28 09:34:51

标签: php yii2

我已在我的Yii2配置文件中将enablePrettyUrl设置为true并将showScriptName设置为false。我的默认控制器名为public

我也有这两条规则:

[
    'pattern' => '<category_id:\w{6}>/<product_id:\w{6}>/<slug:.*?$>/',
    'route' => 'public/product',
    'defaults' => [
        'seo_url' => ''
    ]
],
[
    'pattern' => 'public/<category_id:\d{6}>/<product_id:\d{6}>/<slug:.*?$>/',
    'route' => 'public/product',
    'defaults' => [
        'seo_url' => ''
    ]
]

允许我使用以下网址访问我页面中的产品:

http://example.com/123456/987654/this-is-a-prodduct-example-url

现在,我希望如此:

Url::to(["/product", "category_id" => 123456, "product_id" => 98765, "slug" => "this-is-a-product-example-url"]);

形成任何一个:

/123456/987654/this-is-a-product-example-url
/product/123456/987654/this-is-a-product-example-url
/public/product/123456/987654/this-is-a-product-example-url

但我得到了这个:

/product/?category_id=123456&product_id=987654&slug=this-is-a-product-example-url

为什么会这样,我该如何解决?

1 个答案:

答案 0 :(得分:1)

  1. 在模式中,很可能,你的意思是因为你使用的是数字,而不是字母。
  2. 对于单个控制器操作参数,您可以有多个规则,但在形成URL时只会使用第一个规则。
  3. 您正在使用/ product但它应该始终是内部路由,即控制器/操作或模块/控制器/操作。