Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

joshuamilford's avatar

URL Parsing/Dynamic Routing?

So, I'm working on a knowledge base, and one of the requirements is for the URLs to be as semantic and people-friendly as possible. There are Articles that can belong to one or more Article Categories (which can be nested), that can belong to one or more Product Categories.

It'd be super-easy to do using something like http://example.com/support/product/some-product/category/some-category/article/some-article, but we'd like to remove "product", "category", "article".

Some possible URL scenarios are:

... you can kinda see where this is going.

I've currently got it working, but it feels kinda ugly. I'm basically grabbing the url, and iterating through each scenario, checking all three tables to see what matches where. It definitely works, but I just wanted to see if anyone's done something similar or has any other suggestions.

Thanks!

0 likes
2 replies
ohffs's avatar

Is the article slug unique? If so you could pretty much have any friendly url with the article slug trailing and just grab that?

joshuamilford's avatar

Yep. All slugs are unique across all tables (Product Categories, Article Categories, and Articles). I guess the problem is mostly related to the article categories, since they can be nested, so to reflect that structure, their slugs are allowed to contain slashes. This makes it a little tougher to figure out if the user's viewing an index page for a category (or product).

Please or to participate in this conversation.