Welcome Guest, Not a member yet? Register   Sign In
Using modules in extended controllers
#1

[eluser]KevinSPerrine[/eluser]
I'm extending the default controller with my own because I need a good way to load the pages differently based on whether it is an ajax request or regular http. I've extended the default controller and added these three functions to it, and then all my other controllers extend this class. I have models that need to be loaded, but it doesn't seem that autoloaded models work in the MY_Controller class. So, I added the get_instance() method to the constructor and the models work within the MY_Controller class, but the models fail to load again when I extend the MY_Controller class (eg. if I create a new class that extends MY_Controller, I don't have the ability access the models anymore.) Even if I explicitly load them again in the constructor, I still can't access them through $this. I have to call get_instance() again and access the models that way. Anyone see why?

Code:
//MY_Controller.php
class MY_Controller extends Controller {
    
    var $clean = array();
        
    function MY_Controller()
    {
        parent::Controller();
        $CI =& get_instance();    
        $CI->load->model(array('crop_m', 'admin_m', 'user_m'));

        $this->_initialize();
    }
    
    function _initialize()
    {
        $clean['title'] = 'Arkansas Variety Testing';
        $views = array('content'=>'shared/home',
                         'left'=>'shared/navigation',
                         'footer'=>'shared/footer');
        $CI =& get_instance();
        $clean['variety'] = $CI->crop_m->_get_crop_display_names();
        $this->_set_view($views, $clean);
    }

    function _set_view($view, $data = array())
    {
        if ((!is_array($view)) && (!is_array($data)))
        {
            return;
        }
        else
        {
            foreach ($view as $cntr=>$uri)
            {
                $this->clean[$cntr] = $this->load->view($uri, $data, TRUE);
            }
        }
    }

    function _display()
    {
        $this->load->vars($this->clean);
        $this->load->view('container/head');
        $this->load->view('container/header');
        $this->load->view('container/left');
        $this->load->view('container/content');
        $this->load->view('container/footer');
    }
}

//crop.php
class Crop extends MY_Controller {

    function Crop()
    {
        parent::MY_Controller();
        //unable to access models loaded in this class.
        //I have to access them through get_instance() everytime
        //instead of loading them regularly and accessing them through
        //$this->/*model_name*/->/*function_name*/();
    }

//other methods excluded.
}
#2

[eluser]KevinSPerrine[/eluser]
As usual 10 minutes after posting and asking for help, I think I've fixed the problem.

I was also including 'controller' in the list of autoload libraries, but after removing it from the autoload. Everything seems to be working as expected. So, I'm assuming that extended controllers do not need to be manually loaded?
#3

[eluser]Colin Williams[/eluser]
Quote:So, I’m assuming that extended controllers do not need to be manually loaded?

Not when you use the 'MY_' prefix.




Theme © iAndrew 2016 - Forum software by © MyBB