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

試著忘記壹切 提交于 2019-12-06 12:10:16

问题


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



来源:https://stackoverflow.com/questions/39068783/codeigniter-3-how-to-use-composer-packages-twilio-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!