CodeIgniter Forums

Full Version: MY_Controller and resources
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]theprodigy[/eluser]
If I load a library or helper in the constructor of MY_Controller, is that library or helper usable by the controllers that extend MY_Controller?

MY_Controller:
Code:
function MY_Controller()
{
    parent::Controller();
        
    $this->load->model('MAdmin');
    $this->load->library('form_validation');
        
    if(!$this->session->userdata('logged_in'))
    {
        redirect('admin/login');
    }
    
    $this->data = array();
}

extended class:
Code:
function add()
{
    $data['errors'] = '';
        
    $this->form_validation->set_rules('name', 'Name', 'trim|required');
    
    if ($this->form_validation->run() == FALSE)
    {
        if(array_key_exists("name",$_POST))
        {
            $this->data['errors'] = "<p style='color:red;font-weight:bold;'>Gallery Name is required<p>";
        }
        $this->data['main'] = "galleries/add_gal";
    }
    else
    {
        //get form info
        $this->data['gallery'] = $this->input->post('name');
        $this->data['notes'] = $this->input->post('description');
            
        //add gallery to database
        $this->MGalleryAdmin->add_gallery($this->data['gallery'],$this->data['notes']);
    
            
        //set main variable with what gets displayed
        $this->data['main'] = "galleries/gal_added";
    }

    $this->show_page();
}

I'm asking because this function "add" is throwing an error on
Code:
$this->form_validation->set_rules('name', 'Name', 'trim|required');

is there a way for me to make the resource available to children controllers?

Thanks

El Forum

[eluser]n0xie[/eluser]
Yes. In your construct of the extended class, specify it should load the parent construct.
Code:
class MY_Controller extends Controller{

  function MY_Controller()
  {
    parent::Controller();
    $this->load->library(...);
    $this->load->helper(...);
  }
}

Code:
class Example extends MY_Controller{

  function Example()
  {
    parent::MY_Controller();
  }
}

El Forum

[eluser]theprodigy[/eluser]
the constructor of the extended class calls the parent's construtor.
Code:
class Galleries extends MY_Controller {
    function Galleries()
    {
        parent::MY_Controller();
        
        $this->load->model('MGalleryAdmin');
        
        $this->data['menu_active'] = 0;
    }

I thought that was mandatory which is why I didn't specify it. The constructor of MY_Controller calls the Controller() constructor and loads the resources, the constructor of the extended class calls the constructor of MY_Controller, but for some reason, the resources aren't being able to be used by the extender. Do I need to assign the resources to a class level variable, and use them that way?

Code:
$this->form_val = $this->load->library('form_validation');

Is that what I need to do in order to use them in the extender?

El Forum

[eluser]n0xie[/eluser]
[quote author="theprodigy" date="1244960146"]
Code:
$this->form_val = $this->load->library('form_validation');

Is that what I need to do in order to use them in the extender?[/quote]
No it should just work the way you have it now. :-s

But if you're going to use the library in every controller you might as well load it in your autoload config file. You might try if that works...

El Forum

[eluser]theprodigy[/eluser]
not sure if this makes a difference, but just in case it does, I am running CI with Matchbox.

This shouldn't make a difference, but figured I should probably mention it since what I have should work and isn't.

El Forum

[eluser]theprodigy[/eluser]
OK, got an update.

The module is loading "MY_Controller". The module contains "MY_Controller". The problem is the site itself that the module is on has it's own MY_Controller, and the site's MY_Controller was the one getting extended, not the module's.

So, this seems to be a Matchbox issue, and not a CI issue. I will take this to them.

Thanks for your help.