【问题标题】:Where to put Use statements for Amazon AWS SDK in Codeigniter 3在 Codeigniter 3 中将 Amazon AWS 开发工具包的 Use 语句放在何处
【发布时间】:2020-06-27 10:05:09
【问题描述】:

我正在尝试将 Amazon AWS SDK for PHP 集成到我的 Codeigniter 3 应用程序中。我正在从 Composer 加载 SDK。问题是我不知道在哪里放置 use 语句。

如果我把它们放在这里:

<?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;

这是我的 autoload.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.*"
    }
}

如果我把它们放在类的顶部,我会得到 Trait 'Aws\S3\S3Client' not found。

我不知道还能去哪里!还有其他人成功了吗?

【问题讨论】:

  • 确定你没有配置奇怪的 auto_loading 吗?
  • @ZbigniewMalcherczyk 我不这么认为,但我已经用自动加载的内容更新了我的帖子。
  • 您介意发布您的 composer.json 文件吗?
  • @ZbigniewMalcherczyk 根本没有 - 添加到帖子中。

标签: php amazon-web-services codeigniter amazon-s3 codeigniter-3


【解决方案1】:

最后,通过如下更改,我能够使代码正常工作:

<?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'
        ]);

【讨论】:

    【解决方案2】:

    将供应商放入库中并使用以下源代码制作库文件

    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 配置文件 insie 这放置了密钥和存储桶区域。

    【讨论】:

    • 按照您的建议进行操作仍然会给出错误“找不到 Class 'Aws\S3\S3Client'” - 请在此处查看代码:pastebin.com/s354t4gP
    • 你添加了 autoload.php 文件吗?
    【解决方案3】:

    您应该在使用 vendor 文件的地方要求自动加载文件

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

    参考此链接Reference link

    【讨论】:

    • require_once 不是必需的,因为自动加载会在应用程序文件夹中提供它“启用此设置将告诉 CodeIgniter 在应用程序/供应商中查找 Composer | 包自动加载器脚本/autoload.php。”所有其他作曲家文件都工作文件,因此自动加载工作正常。
    猜你喜欢
    • 2017-08-02
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 2017-03-23
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多