Welcome Guest, Not a member yet? Register   Sign In
Extending the controller class
#7

[eluser]cahva[/eluser]
The purpose of __autoload is that when using PHP5, it will automatically load your classes from the libraries folder if they are the same name. When using this __autoload, you dont actually need MY_Controller at all.

Put the autoload to the config.php as suggested.

Create Admin_Controller.php to libraries folder.
Code:
class Admin_Controller extends Controller {
    function __construct()
    {
        parent::__construct();
        // Do something here eg. check if logged in
        $this->load->library('ion_auth');

        if (!$this->ion_auth->logged_in())
        {
            redirect('login');
        }
    }
}

Create Site_Controller.php to libraries folder.
Code:
class Site_Controller extends Controller {
    function __construct()
    {
        parent::__construct();
        // Do something else here. Maybe load some helpers, Create content that you need sitewide etc..
    }
}

Then in controllers you extend from one of those. Normal pages would use Site_Controller and pages that needs authenticated user extends from Admin_Controller.

controllers/home.php
Code:
class Home extends Site_Controller {

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $this->load->view('home');
    }

}

controllers/backend.php
Code:
class Backend extends Admin_Controller {

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $this->load->view('backend');
    }

}

Try it. Should work(unless I had some typos Wink ). BTW, base controllers dont use index() methods. And always use __construct as constructor(you had some mixed constructors with PHP4/PHP5 style in your code).


Messages In This Thread
Extending the controller class - by El Forum - 10-18-2010, 05:06 PM
Extending the controller class - by El Forum - 10-18-2010, 05:33 PM
Extending the controller class - by El Forum - 10-19-2010, 10:27 AM
Extending the controller class - by El Forum - 10-19-2010, 12:51 PM
Extending the controller class - by El Forum - 10-19-2010, 02:17 PM
Extending the controller class - by El Forum - 10-19-2010, 03:05 PM
Extending the controller class - by El Forum - 10-19-2010, 05:42 PM



Theme © iAndrew 2016 - Forum software by © MyBB