Куда положить заявления об использовании для Amazon AWS SDK в Codeigniter 3 - PullRequest
1 голос
/ 16 марта 2020

Я пытаюсь интегрировать Amazon AWS SDK для PHP в мое приложение Codeigniter 3. Я загружаю SDK из Composer. Проблема в том, что я не могу понять, куда поместить операторы использования.

Если я их здесь поставлю:

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

use Aws\S3\S3Client;

use Aws\Exception\AwsException;

class S3_model extends CI_Model {

  public function __construct()
  {
    parent::__construct();

    //Create a S3Client
    $s3 = new Aws\S3\S3Client([
        'profile' => 'default',
        'version' => 'latest',
        'region' => 'eu-west-1'
    ]);

Я получу класс 'Aws \ S3 \ S3Client' не найден ,

У меня Composer автозагрузка:

$config['composer_autoload'] = TRUE;

А это содержимое моей автозагрузки. php файл:

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

/*
| -------------------------------------------------------------------
|  Auto-load Libraries
| -------------------------------------------------------------------
| These are the classes located in system/libraries/ or your
| application/libraries/ directory, with the addition of the
| 'database' library, which is somewhat of a special case.
|
| Prototype:
|
|   $autoload['libraries'] = array('database', 'email', 'session');
|
| You can also supply an alternative library name to be assigned
| in the controller:
|
|   $autoload['libraries'] = array('user_agent' => 'ua');
*/
$autoload['libraries'] = array('database', 'smartie' => 'smarty', 'session');

/*
| -------------------------------------------------------------------
|  Auto-load Drivers
| -------------------------------------------------------------------
| These classes are located in system/libraries/ or in your
| application/libraries/ directory, but are also placed inside their
| own subdirectory and they extend the CI_Driver_Library class. They
| offer multiple interchangeable driver options.
|
| Prototype:
|
|   $autoload['drivers'] = array('cache');
|
| You can also supply an alternative property name to be assigned in
| the controller:
|
|   $autoload['drivers'] = array('cache' => 'cch');
|
*/
$autoload['drivers'] = array();

/*
| -------------------------------------------------------------------
|  Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
|   $autoload['helper'] = array('url', 'file');
*/
$autoload['helper'] = array('url','utility','postrecycler');

/*
| -------------------------------------------------------------------
|  Auto-load Config files
| -------------------------------------------------------------------
| Prototype:
|
|   $autoload['config'] = array('config1', 'config2');
|
| NOTE: This item is intended for use ONLY if you have created custom
| config files.  Otherwise, leave it blank.
|
*/
$autoload['config'] = array();

/*
| -------------------------------------------------------------------
|  Auto-load Language files
| -------------------------------------------------------------------
| Prototype:
|
|   $autoload['language'] = array('lang1', 'lang2');
|
| NOTE: Do not include the "_lang" part of your file.  For example
| "codeigniter_lang.php" would be referenced as array('codeigniter');
|
*/
$autoload['language'] = array();

/*
| -------------------------------------------------------------------
|  Auto-load Models
| -------------------------------------------------------------------
| Prototype:
|
|   $autoload['model'] = array('first_model', 'second_model');
|
| You can also supply an alternative model name to be assigned
| in the controller:
|
|   $autoload['model'] = array('first_model' => 'first');
*/
$autoload['model'] = array();

Вот мой composer. json

{
    "description": "The CodeIgniter framework",
    "name": "codeigniter/framework",
    "type": "project",
    "homepage": "https://codeigniter.com",
    "license": "MIT",
    "support": {
        "forum": "http://forum.codeigniter.com/",
        "wiki": "https://github.com/bcit-ci/CodeIgniter/wiki",
        "slack": "https://codeigniterchat.slack.com",
        "source": "https://github.com/bcit-ci/CodeIgniter"
    },
    "require": {
        "php": ">=5.3.7",
        "jublonet/codebird-php": "3.1",
        "phpmailer/phpmailer": "^6.0",
        "dg/rss-php": "^1.3",
        "tpyo/amazon-s3-php-class": "^0.5.1",
        "filp/whoops": "^2.4",
        "stripe/stripe-php": "^7.3",
        "aws/aws-sdk-php": "^3.133"
    },
    "suggest": {
        "paragonie/random_compat": "Provides better randomness in PHP 5.x"
    },
    "require-dev": {
        "mikey179/vfsstream": "1.1.*",
        "phpunit/phpunit": "4.* || 5.*"
    }
}

Если я поставлю их на первое место в классе, я получу черту 'Aws \ S3 \ S3Client' не найден.

Я не уверен, где еще до go! Кто-нибудь еще имел успех?

Ответы [ 3 ]

0 голосов
/ 16 марта 2020

поместите vender в библиотеки и создайте файл библиотеки, используя следующий исходный код

include("vendor/autoload.php");
//require 'vendor/aws/aws.phar';

 use Aws\S3\S3Client;

class S3
{
 private $S3;
 public function __construct()
 {
     $ci = &get_instance();
     $this->S3 = S3Client::factory($ci->config->item("aws_credentials"));
 }

 public function listBuckets()
 {
     $result = $this->S3->listBuckets();
     return $result;
 }

, поместите функцию require и используйте все функции lib в контроллере

ПРИМЕЧАНИЕ: - внутри папки config есть файл s3. php его файлы конфигурации s3 ins ie, в котором находятся ключи и область корзины.

0 голосов
/ 16 марта 2020

В итоге я смог заставить код работать, внеся следующие изменения:

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

class S3_model extends CI_Model {

  private $s3;

  public function __construct()
  {
    parent::__construct();

    //Create a S3Client
    $this->s3 = new Aws\S3\S3Client([
        'version' => 'latest',
        'region' => 'eu-west-1'
        ]);
0 голосов
/ 16 марта 2020

Вы должны требовать файл автозагрузки, где вы используете vendor файлы

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

Ссылка на эту ссылку Ссылка на ссылку

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...