用户结霜和 AWS SDK

UserFrosting & AWS SDK

我有以下代码在 UserFrosting 之外按预期工作:

<?php
echo "Hello World.<br>";

require_once '../vendor/autoload.php';

use Aws\Common\Aws;

$aws = Aws::factory('../aws/aws-config.json');
$client = $aws->get('S3');

$bucket = 'my-public-public';

$iterator = $client->getIterator('ListObjects', array(
    'Bucket' => $bucket
));

foreach ($iterator as $object) {
    echo $object['Key'] . "<br>";
}

在我的 UserFrosting 实例上,我成功加载了 aws-sdk-php with Composer:

  - Installing aws/aws-sdk-php (3.19.24)
    Downloading: 100%

使用Aws\Common\Aws;放在initialize.php,下面参考Slim:

use \Slim\Extras\Middleware\CsrfGuard;
use Aws\Common\Aws;

其余代码在控制器中:

    public function readS3(){
        $aws = Aws::factory('../aws/aws-config.json');
        $client = $aws->get('S3');
        ...
    }

我仍然收到以下错误:

Class 'UserFrosting\Aws' not found.

我错过了什么?

如您所见,它正在 UserFrosting\ 命名空间中查找 Aws class,但它显然不在其中!

您需要 每个 文件顶部的 use Aws\Common\Aws; 来引用 class Aws。或者,您可以使用其完全限定名称简单地引用 class:

$aws = \Aws\Common\Aws::factory('../aws/aws-config.json');

我建议花一个小时左右的时间来了解有关 PHP Namespaces. They are an extremely important concept in modern PHP, and are closely related to Composer, autoloading, and the PSR-4 standard 的更多信息。