I have a ‘categories’ table that has the columns ( id, title, slug, parentid ).
I want my url struture to be something like this:
/<category slug>/action
Is it possible with Yii to have the category slug be a rule in the UrlManager. I can’t seem to think of a way to do this in order to have createUrl() still be usable.
Right now I am catching all errors, checking if it is a 404, splitting up the URI and parsing the different sections. This works fine for me but I want to also be able to use createUrl() so that the urls that yii spits out will use the url setup I have designed.
As of now I have controllers for items like: "blogs, videos, podcasts, articles" and I can access items that belong to a category like:
‘/blogs/12/category-slug’ where ‘12’ is the categoryId.
But the end result I want to achieve is more like:
‘/category-slug/blogs/’
If anyone could give me some pointers it would be greatly appreciated. I am sure others have tried this but it would be nice to have a reference to what people think is the "correct" way to achieve the desired results.
Where’s the problem? <slug> can be used just like a standard URL parameter. So when creating your URL, you supply the slug for the URL to generate. You should maybe make sure, that ‘/’ are replaced with e.g. ‘_’ before.
But since my requirements are pretty simple, I did not use it for my project, thus avoiding the (small) overhead.
It’s definitely a great help for more complex/dynamic rules, so if you find that the urlManager config section grows too big, that extension is going to save you.
And you are very kind, but the overhead shouldn’t be so small. Depending on the rules configuration, it could add at least one query for each request. I will implement caching when I have some time.