Welcome Guest, Not a member yet? Register   Sign In
Getting the super-object $CI in Router class?
#1

[eluser]flokky[/eluser]
Hello everyone, I want to load a database model in the Router class, since I'm overriding some of its behaviour, but I'm stuck in loading the super-object $CI using
Code:
//Get the CI super object instance
$ci = &get;_instance();

//Load model
$this->$ci->load->module_model('handler', 'text', 'text');

I'm getting this error..
Code:
Fatal error: Call to undefined function get_instance() in (...)\Router.php

Anyone any idea how I can fix this? Thanks!
#2

[eluser]xwero[/eluser]
The router class is loaded before the CI instance is created so you can't load a model normally.
#3

[eluser]flokky[/eluser]
I really would like to load the database model and do a query on the database (instead of reading the routes from the routes.php file).

What else are my options? There has to be a way, right?
#4

[eluser]xwero[/eluser]
I wonder why you need routes from the database? They are not going to change on a daily base and there not going to be many.
#5

[eluser]flokky[/eluser]
I would like to 'bundle' all my texts on the website, but refuse to create different pages (which basically are all the same). So I've created a generic controller which fetches the URI (e.g. http://www.host.com/index.php/contact_us) and using the routes.php to find that 'contact_us' is mapped to that 'generic' controller, where the appropriate text is fetched and forwarded to the view.

Now I would like to switch from file to database...

Code:
Database structure:
id int,
textTitle varchar,
textBody text,
active int
created date
modified date
link_word varchar

If you have a better idea to do this, please let me know.
#6

[eluser]xwero[/eluser]
If i understand you now your route is something like
Code:
$route['(.+)'] = 'controller/method/$1';
Because you don't want your controller to look like this
Code:
class Some_Controller extends Controller
{
   funtion index{}

   function about_us {}

   function shop {}
}
#7

[eluser]flokky[/eluser]
Not quite. I'll just post my code over here. I think you'll faster understand what I mean.

The controller:
Code:
class Handler extends Public_Controller {
    
    function Handler()  {        
        // Call parent controller
         parent::Public_Controller();
        
         //Load model
         $this->load->module_model('handler', 'text', 'text');    
    }
    
    function index()
    {    
        //Fetch the keyword from the URL
        $keyword = $this->uri->segment(1);
        $result = $this->text->getTextByLinkword($keyword);
        
        if($result->num_rows() > 0) {
            $data['text'] = $result->result();
        } else {
            $text[0]->textTitle = 'No information available!';
            $text[0]->content = 'Please verify if the URL you provided was correct.';
            $data['text'] = $text;
        }
        
        //Display pages
        $data['page'] = $this->config->item('backendpro_template_public') . 'handler_view';
        $data['module'] = 'handler';
        $this->load->view($this->_container,$data);
    }
}

The view:
Code:
<div class="spanner">
    <h1>&lt;?=$text[0]->textTitle;?&gt;</h1>

    <p>&lt;?=$text[0]->content;?&gt;</p>    
</div>

The routes.php file: (handler is the name of the module using Matchbox and handler is also the name of the controller)
Code:
$route['contact_us'] = "handler/handler";
$route['about_us'] = "handler/handler";
$route['thanks'] = "handler/handler";

Example how this would be in the database (example: URL http://host.com/index.php/about_us was requested):
Code:
id = 24
textTitle = "About us"
content = "<b>a lot of html</b>, links and pictures..."
active = 1
link_word = "about_us"

//Query that's executed
Select * from be_text where link_word = "about_us";

Does this makes sense?
#8

[eluser]xwero[/eluser]
you can do
Code:
$route['contact_us|about_us|thanks'] = "handler/handler";
#9

[eluser]flokky[/eluser]
I want to do it dynamically: and lose the routes.php file, since it's not maintainable that way
#10

[eluser]xwero[/eluser]
What do you mean it's not maintainable that way? If you were talking about user created pages i could understand it better but you are talking about static pages. It would be a bit foolish to alter the router class to make it slower because you want to query the database each time a page loads.




Theme © iAndrew 2016 - Forum software by © MyBB