Welcome Guest, Not a member yet? Register   Sign In
Dynamic URI Routing
#1

[eluser]Unknown[/eluser]
Hello peeps.

I'm pretty new to CI and to PHP generally and I'm going through a pretty steep learning curve.

I've hit a bit of a snag with regards to URI routing and I was wondering of if anybody can help.

Is it possible to create dynamic routing rules based on database queries?

For example: If I have a tree structure of categories, that's likely to be changed through an administration system, can I examine the URI and redirect based on index of any matches?

The only solution I've thought of is to store the routing rules in a seperate config file and write to it when any changes are made to the database. I guess that's wouldn't be a bad solution as it's pretty much like caching results. I'd like to know if there's a way to do the routing on the fly though.

Thanks in advance for any help or suggestions.
#2

[eluser]Dam1an[/eluser]
The problem with this, is that the routing takes place before the autoloading, so without some messy hacks, you won't be able to access the database (at least thats my understanding from following the application flow), unless you create a seperate instance, although you'd then have 2 instances of the database for each requests
#3

[eluser]Phil Sturgeon[/eluser]
I would be tempted to either manually load the database class and run it that way, or use native PHP functions to handle it first. That would obviously add some dependencies on your app over which db can be used, but hopefully there is a way to do it with just the DB driver.

When you have your routes from the database, stick em on a 1 hour cache. That will remove any database-related performance issues and speed things up quite a lot.
#4

[eluser]TheFuzzy0ne[/eluser]
Why not store all of your routes in a separate file as you suggested. It can just be a serialized array. Whenever a change is made to the database, the file can be updated. To me it makes sense to update the file when the database is updated (and obviously available), not to try and hack around it not being available.

Hope this helps.
#5

[eluser]xwero[/eluser]
If the urls are created by the user you will probably have a controller that fetches the content so just guide all urls that are unknown to that controller. In that controller method you add an exists check and if it falls trough you can output the content for that url.
#6

[eluser]Unknown[/eluser]
Thanks for the advice peeps.

I think I'll stick with the simplest solution and store routes in their own config file. Then write to the file when changes are made to the tree structure.

I'll let you know how I get on ...

Thanks again




Theme © iAndrew 2016 - Forum software by © MyBB