I’m coding a little learning CMS project and I’ve hit a brick wall that’s stopping me to complete the next step. I know I should be taking KISS (Keep It Simple, Stupid) into account, but I think it would nice, to be able to group pages hierarchicaly.
The problem is that I want page [root]->fruits->tropical->bananas to be accessible only from this url: http://localhost/cms/fruits/tropical/bananas/. What I came up with until now is that cms table has a parent field that points to its parent. The question is: How to parse uri adress and select a row from DB with as few queries/efficiently as possible?
Table structure:
Id
Slug
...
...
...
ParentId
All help and advice is kindly accepted.
Here is the table structure that I used for testing this –
Note the unique key on (parent_id, slug). This is key to getting the best performance from the following query. I tested this with 50k rows and it still returned in less than 1ms for a five slug path –
/cms/slug-1/slug-2/slug-3/slug-4/slug-5/Here is the PHP code that I have come up with to build a suitable query –