Welcome Guest, Not a member yet? Register   Sign In
Extending CI_Loader - how to access CI Models?
#1

[eluser]Sam Granger[/eluser]
Hello!

I want to extend the CI_Loader with MY_Loader. In MY_Loader, I want to access the CodeIgniter Agent Model. How can I achieve this?

Thanks in advance!
#2

[eluser]WanWizard[/eluser]
You can't do much in the loader, the class is loaded at a moment when CI isn't fully up and running, so things like 'get_instance()' won't work.

I don't think MY_Loader is the place to put 'user' code, what is it exactly that you want to achieve?
#3

[eluser]Sam Granger[/eluser]
I want visitors on mobile phones to see a different view "theme" - currently have a getTheme function in a Model named Theme. This function checks agent->is_mobile and from there it returns which folder the controller should load its views from ($this->load->view($this->theme->getTheme() . 'view_file'))

I was wondering if I could just override the view function with this functionality - not sure what the best solution would be in this case?
#4

[eluser]WanWizard[/eluser]
I don't think this is the way to go.

I would rather opt to create two folders in the views directory, one for browser views, and one for mobile views. Then in your MY_Controller, do
Code:
define('VIEW_DIR', $this->theme->getTheme() ); // returns the name of the views directory, ending with a /

Assuming that your MY_Controller loads and runs before you call your first view, you could use MY_Loader like this:
Code:
function view($view, $vars = array(), $return = FALSE)
{
    if ( defined('VIEW_DIR') )
    {
        parent::view(VIEW_DIR . $view, $vars, $return);
    }
    else
    {
        parent::view($view, $vars, $return);
    }
}

Then in your controllers you can keep using
Code:
$this->load->view('view_file');

which would load the view_file.php from the directory returned by getTheme().
#5

[eluser]Sam Granger[/eluser]
Nice, I'm already extending the Controller so it's a nice solution, will give it a try. Thanks! Smile




Theme © iAndrew 2016 - Forum software by © MyBB