Welcome Guest, Not a member yet? Register   Sign In
Routing via Database?
#1

[eluser]JasonS[/eluser]
Hello, I am making a small CMS, it has a page system which currently routes as follows.

app/page/page_name

Is there a way to set custom routing without using php to write a new routing file?

I was under the impression that routing is activated before any custom app code kicks in? Ideally I would like the site to query the db to check for pages. I.e. if the url is.

app/page_name

The page_name controller doesn't exist, as a result the page queries the db and if the page exists it will route to.. app/page/page_name

I hope that makes sense.
#2

[eluser]Adam Griffiths[/eluser]
I did this for an old CMS I made.

Code:
// BEGIN FRESH DYNAMIC ROUTING

$connection = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('db_name', $connection);

$query = mysql_query('SELECT * FROM routes');

while($result = mysql_fetch_array($query))
{
    $route[$result['fake_route']] = $result['real_route'];
}

mysql_close($connection);

// END FRESH DYNAMIC ROUTING

It's pretty simple, all you need inside the table is an id, the "fake" route (the first route in th CI route), and the real route (the part after the = sign).

Hopefully I made sense.
#3

[eluser]moodh[/eluser]
Problem with that is that you'll be using double connections throughout your entire application, which isn't very neat ;p
#4

[eluser]JasonS[/eluser]
Thanks for the replies, that would be a good short term solution however it doesn't seem like the most efficient long term solution Smile
#5

[eluser]xwero[/eluser]
You don't need to rewrite the whole file if there is something distinctive on the line you actually need.
Code:
$file = APPATH.'/config/routes.php';
$lines = file($file);

foreach($lines as $key=>$line)
{
   if(preg_match('/app\/\$1/',$line)) // controller and method that need to be used
   {
     // do your stuff
     $lines[$key] = $altered_line;
     file_put_contents($file,implode('',$lines));
     break;
   }
}
#6

[eluser]Phil Sturgeon[/eluser]
xwero's method would be nice to run as a cron job for minimum file load, but if you want to do this on-the-fly then make an extension to the router library. Then you can make a database call using your built in CI config and database connection.

Best place to do it would be around line 126, a similar modification to my Matchbox with modular routes in CodeIgniter blog entry.




Theme © iAndrew 2016 - Forum software by © MyBB