Welcome Guest, Not a member yet? Register   Sign In
Sloppy Database URI Routing for 2.0.1
#1

[eluser]exit[/eluser]
Hi guys,

I'm sure there are tons of better ways to create a more dynamic than this one, but since I couldn't find one myself I decided to print this one here. And sorry for my bad English Smile

1. Create a MY_Router in Core/ and extend it to CI_Router

2. I'm not sure how to make your own routes talk to the CI_Router itself so instead I basically copied CI_Router to MY_Router.

3. If you intend to use lots of database queries it's best to use this:

Code:
function __construct()
    {
        parent::__construct();
        $this->config =& load_class('Config', 'core');
        $this->uri =& load_class('URI', 'core');
        log_message('debug', "Router Class Initialized");
        
        require_once(BASEPATH.'database/DB'.EXT);
        $this->db = DB();
    }

Now you have your $this->db just like in the controller.

4. Fetching your database routes
Create a new function where you fetch your routes from the database and return an array similar to that of config/routes.php.

For example:


Code:
private function dbRoutes()
    {
        $route = array();
        $query = $this->db->get('routes');
                
        foreach($query->result() as $row) {
             $route[$row->uri] = $row->referrer;
             if($row->default)
                 $route['default_controller'] = $route[$row->referrer];
        }

        return $route;
    }

4. Adding to _set_routing()

Now all you have to do is add the new function to _set_routing()

If you want to you can remove the

Code:
// Load the routes.php file.
        @include(APPPATH.'config/routes'.EXT);
and instead just do a
Code:
$route = $this->dbRoutes();
or you can just add them together.

That's it now you have dynamic URI routing on your application without having to touch the system folder.

If their is someone who has a better method of doing this please share it with us others cause I'm sure this is a big problem for many.




Theme © iAndrew 2016 - Forum software by © MyBB