26

I've followed this article: http://philsturgeon.co.uk/blog/2012/05/composer-with-codeigniter

But I get Fatal error: Class 'Buzz\Browser' not found.

What is missing from his post?

My controller or application isn't namespaced. I was hoping to just be able to use that one package in one controller's action in a non-namespaced framework.

j0k
  • 22,600
  • 28
  • 79
  • 90
Tjorriemorrie
  • 16,818
  • 20
  • 89
  • 131

7 Answers7

51

For CodeIgniter 3.x and composer, it's suggested to just set $config['composer_autoload'] to TRUE or a custom path in application/config/config.php.

It seems that CI assumes the vendor directory is within the application directory. That wasn't my case. I did the following:

$config['composer_autoload'] = 'vendor/autoload.php';

SeanWM
  • 16,789
  • 7
  • 51
  • 83
40

Credit to @jmadsen

This is possible by just getting the order of loading correct:

/*
 * --------------------------------------------------------------------
 * LOAD THE BOOTSTRAP FILE
 * --------------------------------------------------------------------
 *
 * And away we go...
 *
 */
// Composer Autoloader
require FCPATH . 'vendor/autoload.php';

require_once BASEPATH.'core/CodeIgniter.php';

/* End of file index.php */
Tjorriemorrie
  • 16,818
  • 20
  • 89
  • 131
11

Well in Codeigniter3.x you can easily do that by going into the application/config/config.php and look for this line

$config['composer_autoload'] = FALSE;

Make sure you set it to TRUE and right after it you require_once this:

require_once APPPATH.'vendor/autoload.php';

So you get something like this:

/*
|--------------------------------------------------------------------------
| Composer auto-loading
|--------------------------------------------------------------------------
|
| Enabling this setting will tell CodeIgniter to look for a Composer
| package auto-loader script in application/vendor/autoload.php.
|
|   $config['composer_autoload'] = TRUE;
|
| Or if you have your vendor/ directory located somewhere else, you
| can opt to set a specific path as well:
|
|   $config['composer_autoload'] = '/path/to/vendor/autoload.php';
|
| Note: This will NOT disable or override the CodeIgniter-specific
|   autoloading (application/config/autoload.php)
*/
$config['composer_autoload'] = TRUE;
require_once APPPATH.'vendor/autoload.php';

Just make sure you have your vendor folder in the application folder and you are good to go.

I recently found out that you can just set $config['composer_autoload'] = TRUE; and put your vendor folder in the application folder and that's it.

For those who would want your vendor folder outside the application folder. You can make it happen in this way: for example you want to place it in the root folder.

TIP: it has been described in the comment already

$config['composer_autoload'] = '/path/to/vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Composer auto-loading
|--------------------------------------------------------------------------
|
| Enabling this setting will tell CodeIgniter to look for a Composer
| package auto-loader script in application/vendor/autoload.php.
|
|   $config['composer_autoload'] = '/path/to/vendor/autoload.php';
|
| Note: This will NOT disable or override the CodeIgniter-specific
|   autoloading (application/config/autoload.php)
*/
$config['composer_autoload'] = FCPATH .'vendor/autoload.php';

Where FCPATH is codeigniter's defined constant for the root folder.

I guess this helps.

Developer Kwame
  • 168
  • 1
  • 5
  • 14
9

EDIT: Damn, I just said pretty much exactly the same thing as @Tjorriemorrie

If you've followed all the other directions correctly, all you need to do is add the following code near the very the end your index.php file:

/*
 * --------------------------------------------------------------------
 * COMPOSER AUTOLOAD
 * --------------------------------------------------------------------
 */
include_once './vendor/autoload.php';

...just make sure you slot it in before the CodeIgniter Bootstrap file is called:

/*
 * --------------------------------------------------------------------
 * LOAD THE BOOTSTRAP FILE
 * --------------------------------------------------------------------
 *
 * And away we go...
 *
 */
 require_once BASEPATH.'core/CodeIgniter.php';
TunaMaxx
  • 1,782
  • 12
  • 18
7

You can add directly the Composer Autoloder in your controller:

// Composer Autoloader
require FCPATH.'vendor/autoload.php';
Kinobi
  • 192
  • 2
  • 7
  • I did include it in the index.php; the problem is the class is not found using FQN. Do you know how to do that? – Tjorriemorrie Dec 13 '12 at 08:32
  • It didn't work for me either in index.php. That's why I put it directly in the controller. You can try to extend CI_Controller and add the Composer Autoloader in the constructor ? – Kinobi Dec 14 '12 at 08:50
5

There are two ways you can autoload the class file which is required using composer.

  1. Add below line in index.php in the root directory.

    require FCPATH . 'vendor/autoload.php';
    
  2. Or autoload directly in the controller where you want to use.

    defined('BASEPATH') OR exit('No direct script access allowed');
    
    require FCPATH . 'vendor/autoload.php';
    
    class Home extends CI_Controller {...}
    
rob006
  • 21,383
  • 5
  • 53
  • 74
1

I'm using Kenjis codeigniter composer package, and it puts the vendor directory off of the root. Since there is no predefined constant (that I know of) for the root, I used the following:

$root = getcwd();
$config['composer_autoload'] = "$root/vendor/autoload.php";
James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78