UrlManager rules to display Category/subCategory/Product using slugs

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:


and route that to ‘product/view’ controller action.

Right now, I’m using this rule:


    '/<catSlug:w+>/<subCatSlug:w+>/<proSlug:w+>' => 'product/view',


and this is able to generate url’s in desired format (eg. www.mysite.com/some-category/some-subcategory/some-product.html) but, after clicking on link the system throws 404 error with message:

Unable to resolve the request "some-category/some-subcategory/some-product".

I would really like to achieve this, but I’m also open to other suggestions too.

Thanks in advance :)

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 .

Thanks for reply.

I agree that your solution is easier to work with, but I have requirement to try mimic folder structure of static web site.

There will never be nested subcategories, and all my slugs are unique. More job to do, but… I’ll have more satisfied customer ;)

Also, I found answer to my question.


    '<catSlug>/<subCatSlug>/<proCat>' => 'product/view',


before it was:


    '<catSlug:w+>/<subCatSlug:w+>/<proCat:w+>' => 'product/view',


which is wrong. But after removing :w+ it works as expected.

As you wish, but i still remain at my initial opinion, this approach is very limited and it will give you allot of headaches.

The assumption that you won’t have nested categories is kind of wrong, you never know how the website evolves in future .



    '<category:[a-z0-9\-_\.]+>/<subcategory:[a-z0-9\-_\.]+>/<product:[a-z0-9\-_\.]+>' => 'product/view',


Will allow you to have url like /my-super-category/with-a-super-subcategory/showing-this-fancy-product.html

and you will $_GET :

$_GET['category'] = my-super=category



You need to study more the way urlManager works and a bit more the regular expressions in php .


I like your idea…

can you please provide some code snippet…