CodeIgniter Forums

Full Version: Dynamic Routes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Adam Griffiths[/eluser]
This has been discussed to death on the forums but none of the topics I read through answered my question, so here goes.

Quick question on last line.

I am developing a CMS in CI and need to make use of dynamic routes. At first I thought it was easy as using the DB class, but obviously I can't use it in a config file.

Then I had the idea of using a hook, but then I have no experience with hooks, and since I'd have to use an early system hook, no of the classes would have been loaded by then.

The next idea was to search the forums, that didn't really help me either.

So, how would I go about creating a hook to get dynamic routes? Is a hook the best way to do it?

I don't think I explained it well enough...I have a database table called routes, inside there I have fake_route and real_route, fake_route is the URI the user will see, and real_route is the route we map it to.

I need to loop through a db query to grab all of the routes and process them however.

For example I could have /home that will map to pages/index/home, whereas /blog will map to blog/ or similar.

Any ideas on my dilemma?

Basically, what's the best way to implement dynamic routes taken from a DB?

All comments appreciated!

El Forum

[eluser]richthegeek[/eluser]
use the built in PHP MySQL functions rather than the CI DB class?

El Forum

[eluser]Adam Griffiths[/eluser]
It didn't work the first time, but then I re-read my post and had to hit myself. I forgot to connect to the database in the first place...I know, dumb!

Anyway I have a version up and running now. Thanks!

El Forum

[eluser]narkaT[/eluser]
I had exactly the same issue a few days ago.

I'm currently extending the router class to call an alias-controller if no "real" controller is found.

an alternative to extending the router class would be this.

Greetings
Jan

El Forum

[eluser]Colin Williams[/eluser]
I would have a controller that handles unresolved requests, and overload the method of the router class that would typically show_404(), but instead have it go to the controller you set (probably best to use something defined in routes.php and not hardcode it right there in MY_Router.php).

The problem with opening a MySQL connection, as far as I see (and I've done this before) is that you end up with 2 connections per request. Might not cripple your app, but it isn't ideal.

El Forum

[eluser]narkaT[/eluser]
[quote author="Colin Williams" date="1222317389"]I would have a controller that handles unresolved requests, and overload the method of the router class that would typically show_404(), but instead have it go to the controller you set.[/quote]
that's what I'm actually doing there Wink

[quote author="Colin Williams" date="1222317389"](probably best to use something defined in routes.php and not hardcode it right there in MY_Router.php)[/quote]

yes, but it's not possible to create a route, that only matches non-existing controllers.
so extending the router class is the only alternative (as far as I know).

Greetings
Jan