Ok, first of all, I know that title of this topic is a little bit unclear, so let me try to explain what I need to accomplish
I have table that holds categories and subcategories, and another table that holds products. Both tables have “slug” field that I want to use to achieve more user friendly url’s.
The Big Idea is as fallowing:
for example:
category slug: some-category
subcategory slug: some-subcategory
product slug: some-product
I need UrlManager rule that will (in conjunction with CreateUrl) generate url in the fallowing format:
The approach is not the best you can get in my opinion, because it introduces extra complexity in everything you are doing .
Another bad thing is that, for example if you have multiple nested categories, you’d have a url like:
/category/sub-category/sub-sub-category/product which is very ugly, and of course you will never make the url manager to handle all the possible variations .
Another issue with your approach is that you generate slugs for category/subcategory, and you query using these slugs, this means your categories/subcategories needs to be unique. This is a huge drawback .
This is how i do it:
I have a table for categories(cat_id|parent_id|url) a table for products(product_id) and a table for product_to_category(product_id|category_id)
In front-end, the urls are simple as possible
/category-nice-url-123 -> will list all the products from this category, and you can also query the subcategories of this category
/product-nice-url-123 -> will show the details for the product with ID 123 .
After lots of tests, i assure you this is the way to go .