Conditional Auto-Loading of Libraries in CodeIgniter

I’ve recently begun digging into CodeIgniter for a new project I’m working on. One of the first things that strikes me is the lack of a good way to set up your database tables. The standard method seems to be, in a nutshell: point and click in PHPMyAdmin or import a SQL file from the command line.

So, step 1: create an Install controller. This is a pretty basic controller that you access at (and which you’ll disable after developing the site). All it does is create the database tables you’ll need and add any initial values you might want. You can figure the rest of it out on your own.

But I have a problem. I want to auto-load the session library for all of my controllers. That’s easy enough: just add $autoload['libraries'][] = 'session'; to your autoload.php file. The session library, though, requires a properly-defined table to exist in the database before it can be used.* If you auto-load it, though, it will also auto-load for the Install controller, which won’t work, since the tables haven’t been installed, yet.

*Technically, it can work without a database, but then I’d have to keep changing a configuration setting every time I want to re-install the database, which I’m doing rather frequently in the early stages of building this app.

So, I made a hack to get around that. Please note that I do not recommend this on a production site. This is for development purposes only.
Continue reading “Conditional Auto-Loading of Libraries in CodeIgniter”