T
T
timehollyname2021-10-14 02:42:25
Laravel
timehollyname, 2021-10-14 02:42:25

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


How can I get the entry with ID: 3 if there is a full route: /media/news ? Chet nothing comes to mind. Or, for example, how to get a record with ID: 2, if there is a route: / media

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin B., 2021-10-14
@Kostik_1993

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 question

Ask a Question

731 491 924 answers to any question