Question

In my application/config/config.php, I want to autoload my library package:

/*
| -------------------------------------------------------------------
|  Auto-load Packges
| -------------------------------------------------------------------
| Prototype:
|
|  $autoload['packages'] = array(APPPATH.'third_party', '/usr/local/shared');
|
*/

$autoload['packages'] = array('../../lib');

The relative path did not work, so I got the current directory of my index.php and tried that:

$autoload['packages'] = array(getcwd() . '/../../lib');

Finally, I just stuck the absolute path in there:

$autoload['packages'] = array('/Users/.../lib');

None of these worked. Am I missing something? Because the documentation is quite sparse.

Was it helpful?

Solution 2

To fix this I added a modified autoloader right before the require_once statement in index.php:

spl_autoload_register(function($class) {
    if (strstr($class, 'CI') || strstr($class, 'MY')) {
        return;
    }

    $class = ltrim($class, '\\');
    $filename  = '';
    $namespace = '';

    if ($last_ns_pos = strripos($class, '\\')) {
        $namespace = substr($class, 0, $last_ns_pos);
        $class = substr($class, $last_ns_pos + 1);
        $filename  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
    }

    $filename .= str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';

    require strtolower($filename);
});

The original autoloader can be found here.

Honestly, no matter what I tried with CodeIgnitier, loading third-party packages or drivers just would not work.

OTHER TIPS

I had a similar problem a while ago. I had to create a library to load a driver. I fixed it with the following.

In /application/config.php

$autoload['libraries'] = array('callcache');
//callcache is loaded from application/libraries/Callcache.php

In /application/libraries/Callcache.php

<?php
class Callcache  {
    public function __construct($config = array())
    {
        $ci = &get_instance();
        $ci->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
    }
}

Hopefully that helps you out, even though you're trying to load a package and not a driver (via a library). I can't remember where I found the info for this and still learning CI myself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top