Answer the question
In order to leave comments, you need to log in
Nested routes stored in the database?
I want to implement dynamic routes that, when followed, will display certain content.
I have a table: id | parent_id | route | content
Suppose there are 3 records in the database:
id | parent_id | route | content
-----------------------------------------
1 | NULL | / | content1
-----------------------------------------
2 | 1 | media | content2
-----------------------------------------
3 | 2 | news | content3
Answer the question
In order to leave comments, you need to log in
Take the path of your URI, split it into parts on the slash. Take the last element from the resulting array. Profit! Problem 1 solved
But yes, now the URI can be anything and the page with the media alias can be accessed by any link like
/apple/banana/media
/foo/media
/media
Therefore, you need to follow the SEO rule so that there are no duplicates. To do this, after the step above, you need to get all the parents in the chain, compose the desired (correct) URI, check it with the one you went to, if we do nothing the same and show the page. If different, then you need to redirect to the one that was generated
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question