Steven M Steven M -4 years ago 104
PHP Question

Codeignter 3: How to use composer packages? (Twilio SDK)

What I did so far:



I am pretty familiar with CI, but new to composer and the twilio SDK. Reading some tutorials and docs I managed to install composer and the twilio package. However the
/vendor
folder is parallel to my CI installation:

/var/www/html/
- application
- system
- vendor


I have therefore edited the config.php setting the path like this:

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


In my controller tried to use the SDK as documented in the Twilio SDK:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Test extends CI_Controller {

use Twilio\Rest\Client;

public function twilio()
{
$client = new Client($AccountSid, $AuthToken);

}

}


However I get back an error:

Fatal error: Test cannot use Twilio\Rest\Client - it is not a trait in /var/www/html/application/controllers/Test.php on line 6


Question: How do I correctly use Composer Packages in Codeingiter 3?


Answer Source

In Config.php, put these lines of code

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

and make changes in your controller like-

<?php

use Twilio\Rest\Client;

defined('BASEPATH') OR exit('No direct script access allowed');

  class Test extends CI_Controller {

  public function twilio()
  {
    $client = new Client($AccountSid, $AuthToken);

  }
}

Refer: http://theprofessionguru.com/android/how-to-load-composers-vendor-autoloadphp-in-codeigniter

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download