We use a system where we check the incoming URI against the database and see where to send the user. This way, we can completely detach the URI from the system internals (such as IDs).
We match the incoming URI against the uri
field in the database and load the correct function from the correct class (controller) or redirects. It also has the option to send parameters to the function.
Let me show by example
+---------+---------+----------+----------+--------+
| uri | class | function | new_uri | vars |
+---------+---------+----------+----------+--------+
| faq | support | faq | | |
+---------+---------+----------+----------+--------+
| sandwich| content | news | | 1 |
+---------+---------+----------+----------+--------+
| banana | | | sandwich | |
+---------+---------+----------+----------+--------+
Case 1:
The user requests test.com/faq, the URI router finds a match on uri = 'faq' and tells the system to diplay whatever faq()
in the support class or controller displays.
Case 2:
The user requests test.com/sandwich which is a shortcut for a news story with the ID 1. So the URI router sends a call to news()
in the content class and we can retrieve the id 1 from a variable like $_GET['vars'][0]
.
Case 3:
The user requests test.com/banana, which has been moved to test.com/sandwich since we are not in the banana industry anymore. So the URI router politely sends a 301 and redirects the user to the correct place.
This method has many ways to expand, e.g. by allowing wildcards in the uri field etc. Hope it can be of some value to you too.