Welcome Guest, Not a member yet? Register   Sign In
Autoload correspondig model
#1

[eluser]Escee[/eluser]
Hi,

Is it possible (with an easy solution) to autoload a model corresponding the controller?

Let's say i've got this setup:
> controllers/welcome.php
> models/welcomeModel.php

So CodeIgniter looks if there's a 'welcomeModel' file, and use that as a model within the 'welcome' controller?

Stefan
#2

[eluser]Seppo[/eluser]
You can extend controller class to MY_Controller and use it's constructor to look for the model...
Something like this
Code:
<?php
class MY_Controller extends Controller {
    function MY_Controller()
    {
        parent::Controller();
        $classname = get_class($this);
        if (is_file(APPPATH . 'models/' . $classname . 'Model.' . EXT))
        {
            $this->load->model($classname . 'Model');
        }
    }
}
?>
#3

[eluser]Escee[/eluser]
Damn, stupid of me that I didn't think about something like that before ><

Thanks! Smile




Theme © iAndrew 2016 - Forum software by © MyBB