Welcome Guest, Not a member yet? Register   Sign In
SOLUTION: Database Class / get_instance() in Router.php
#1

[eluser]m4ikel[/eluser]
Solution Below:

Hello people,

I'm trying to modify the CI Router system to fetch the homepage details from a database. This database contains a table "ci_pages" with all the pages created through the backoffice. In here there is a row "homepage" which should output 0 or 1 depending on the users setting.

Now here's the catch, I've tried modifying the Router.php inside the library but I noticed that I am not able to call the CI get_instance() method. With other librarys that I worked with I loaded "helpers" to pull siteid, theme, ect ect. from the database. Main goal behind this is to do this as clean as possible, leaving most of the CI core untouched, but atm this isn't working for me.

Does anyone have any suggestions in which way I could hack into the libary/router.php and make it pull records from the database without any dirty hacks/tricks.

Thanks in advance!
#2

[eluser]WanWizard[/eluser]
The router is loaded long before the CI instance (= the controller to route to) is available.

I haven't seen a solution for this problem, lots of people tried it. Some have solved it by generating the routes from the database after a change. If you run a search here, you'll probably find lots of people attempting this.
#3

[eluser]m4ikel[/eluser]
Thanks for your reply,

Alright then, I'll see if there is a way to modify the CI systems core to handle this differently, maybe we can switch around some libarys which most of them are not depending on each other anyways. If anyone has another suggestion in the main time, be free to post it.
#4

[eluser]m4ikel[/eluser]
Been looking into the way CI initializes the router. Making the CI get_instance() available inside the router.php is just a small change:

System/codeigniter/CodeIgniter.php

Remove at line: 99
Code:
$RTR =& load_class('Router');

Put the following at line: 148

Code:
// Load router class
$RTR =& load_class('Router');


Which makes the get_instance() available inside the router.php, only issue now is that it loads the database class with help of the router class

Code:
$class  = $RTR->fetch_class();".

which makes use of the $class var:

Code:
$CI = new $class();

After this the database will be loaded as

Code:
class_exists('CI_DB');

will return true, meaning database lib has been loaded.
#5

[eluser]m4ikel[/eluser]
Solution to get the database driver running in the Router.php

NOTICE: This is a change on CI Core level, and this change will be overwriten by a system update. So please be aware of this! Also change this at your own risk.

CI VERSION: 1.7.2

FILE: system/codeigniter/CodeIgniter.php

Change Line: 91 -> 100
Code:
/*
* ------------------------------------------------------
*  Instantiate the base classes
* ------------------------------------------------------
*/

$CFG =& load_class('Config');
$URI =& load_class('URI');
$RTR =& load_class('Router');
$OUT =& load_class('Output');

To:

Code:
/*
* ------------------------------------------------------
*  Instantiate the base classes
* ------------------------------------------------------
*/

$CFG =& load_class('Config');
$URI =& load_class('URI');
//$RTR =& load_class('Router'); Added db support to Router.php -> see line: 150
$OUT =& load_class('Output');

Change Line: 147 -> 150

Code:
// Load the base controller class
load_class('Controller', FALSE);

// Load the local application controller

To

Code:
// Load the base controller class
load_class('Controller', FALSE);

// Load Router.php class
$RTR =& load_class('Router');

// Load the local application controller

Now inside the router.php load the database by doing the following:
Code:
$db =& load_class('Loader');
$db->database();

$ci =& get_instance();

Which you can simply use like normal

Code:
$ci->db->from('my_table');
$ci->db->where('my_id',2);
$ci->db->where('my_page',1);
$q = $ci->db->get();

ect. ect.


Now you are able to use the CI database inside the Router.php class. I havn't noticed any issues after changing this. But please report when they occur inside this topic.

Issues/Notice:

- get_instance() : Because of the hack get_instance() is loaded before all core elements have been loaded, you will be required to load them (if possible) manually inside the Router.php.
#6

[eluser]@MaxG[/eluser]
Hey,

What do I have to do, when I want to use it With Codeigniter Version 2.1.3 ?

Greets Smile
#7

[eluser]Mirge[/eluser]
The last reply before yours is nearly 2 years old.
#8

[eluser]@MaxG[/eluser]
Could nobody help me? Smile

Greet




Theme © iAndrew 2016 - Forum software by © MyBB