CodeIgniter 2 predates Composer and has no hook for a third-party autoloader: its loader looks for a file named after the class, in one of two directories, with no namespace anywhere. Getting a Composer package into a CodeIgniter application therefore takes two pieces — the autoloader has to run before the framework boots, and the package needs a thin library in front of it.
// index.php — the last two lines, in this order
require_once __DIR__ . '/application/third_party/vendor/autoload.php';
require_once BASEPATH . 'core/CodeIgniter.php';
// application/libraries/Applog.php — what the loader can actually find
class Applog
{
public $log;
public function __construct()
{
$this->log = new MonologLogger('catalogue');
$this->log->pushHandler(
new MonologHandlerStreamHandler(APPPATH . 'logs/import.log', MonologLogger::WARNING)
);
}
}
// in a controller
$this->load->library('applog');
$this->applog->log->addWarning('sku not found', array('sku' => $sku));
Setting vendor-dir to application/third_party in composer.json keeps the dependencies out of the document root, which matters more than usual here because CodeIgniter’s index.php is the document root. The wrapper class is the part that cannot be designed away: the loader instantiates by file name and hands over an optional config array, so a namespaced class will never be reachable through $this->load->library(). The cost is one edit to index.php, which is a framework file — it has to be reapplied at every CodeIgniter upgrade, so it belongs on the upgrade checklist rather than in anyone’s memory.