1

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?

1
  • Maybe ['public/product', ...] instead of ['product', ...]? Commented Apr 28, 2015 at 10:00

1 Answer 1

1
  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.
Sign up to request clarification or add additional context in comments.

4 Comments

For 1, that is actually correct, as the id could contain letters too. For 2 and 3, yes, and I'd expect the first rule to be matched and used, but that is not the case. I also tried with /public/product (with annd without slash at the beginning) and the result is the same.
What's empty seo_url for?
I want it to default to '' instead of null if I don't pass it when forming a URL.
You can specify default in action i.e. public function actionSomething($seo_url = '')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.