Codeigniter 3: How to use composer packages? (Twil

2020-07-24 06:41发布

问题:

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?

回答1:

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