alexandernst
alexandernst

Reputation: 15099

Forming URLs in Yii2

I have set enablePrettyUrl to true and showScriptName to false in my Yii2 config file. My default controller is called public.

I also have those 2 rules:

[
    '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' => ''
    ]
]

that allow me to access a product in my page with the following URL:

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

Now, I'd expect this:

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

to form any of those:

/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

but instead I'm getting this:

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

Why is that and how can I fix it?

Upvotes: 1

Views: 171

Answers (1)

Sam Dark
Sam Dark

Reputation: 5291

  1. In pattern, most probably, you've meant since you're using digits, not letters.
  2. For a single controller-action-parameters you can have multiple rules but only the first one will be used when forming URL.
  3. You're using /product but it should always be internal route i.e. controller/action or module/controller/action.

Upvotes: 1

Related Questions