Welcome Guest, Not a member yet? Register   Sign In
Database driven routes in CodeIgniter with caching
#1

[eluser]neillyons.co[/eluser]
A faster, simpler and easier solution to database driven routes in CodeIgniter.

Don't know what I'm on about? Read my previous post on database driven routes.

Prerequisites

* CodeIgniter (obviously ;-P )
* Datamapper

Step 1

Define 3 new methods in your routes model (..system/application/models/route.php).
Code:
function save()
{
    parent::save();
    $this->cache();
}

function delete()
{
    parent::delete();
    $this->cache();
}

/**
* Writes contents of database table to a cache file.
*
* @return void
*/
function cache()
{
    $data[] = "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');";

    foreach($this->get()->all as $value)
    {
        $data[] = '$route["' . $value->route . '"] = "' . $value->controller . '";';
    }

    $output = implode("\n", $data);

    write_file(BASEPATH . "cache/routes.php", $output);
}
This will write the contents of the routes table as a PHP array to a file called routes.php in the cache folder (make sure this folder is writeable). This file would be deleted an rebuilt whenever an insert, update, or delete operation is performed on the routes database table.

Step 2

Insert the code below into the routes.php file ( ..system/application/configs/routes.php ) after the scaffolding trigger.
Code:
include_once BASEPATH . "cache/routes.php";
If anyone implemented the original solution let me know what you think of this.


Messages In This Thread
Database driven routes in CodeIgniter with caching - by El Forum - 05-04-2010, 05:12 PM
Database driven routes in CodeIgniter with caching - by El Forum - 05-31-2010, 03:41 AM
Database driven routes in CodeIgniter with caching - by El Forum - 02-27-2011, 11:25 AM
Database driven routes in CodeIgniter with caching - by El Forum - 02-27-2011, 11:26 AM
Database driven routes in CodeIgniter with caching - by El Forum - 02-27-2011, 11:41 AM
Database driven routes in CodeIgniter with caching - by El Forum - 03-03-2011, 07:25 AM
Database driven routes in CodeIgniter with caching - by El Forum - 08-10-2011, 12:53 PM
Database driven routes in CodeIgniter with caching - by El Forum - 01-31-2013, 03:51 AM



Theme © iAndrew 2016 - Forum software by © MyBB