Citizen
Citizen

Reputation: 12957

Can I make an exception to codeigniter autoload?

My codeigniter site autoloads sessions. I have an XML API page that I created but I'm getting a session error because of that autoload. I would prefer not to load sessions on this controller but I don't want to have to load sessions manually on all of my other controllers. Can that be done?

Upvotes: 1

Views: 483

Answers (1)

No Results Found
No Results Found

Reputation: 102774

Use a base controller to load the session class rather than autoload.php and have your controllers extend it. More information here: http://ellislab.com/codeigniter/user-guide/general/core_classes.html

// application/core/MY_Controller.php

class MY_Controller extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->library('session');
    }

}

// you may add additional base controller classes here

You must extend this controller with the ones that you want to have access to the session class, so unfortunately you will have to make some edits to your existing controllers:

class UserController extends MY_Controller {

    public function index()
    {
        // session class is loaded
    }

}

Other controllers can continue to extend CI_Controller and the session class won't be loaded.

I use this method for all my CI projects and rarely use the autoloader.php, it allows much more flexibility.

Upvotes: 2

Related Questions