UserFrosting & AWS SDK

假如想象 提交于 2020-01-15 05:14:10

问题


I have the following code working as expected outside 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>";
}

On my UserFrosting instance I managed to successfully load aws-sdk-php with Composer:

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

use Aws\Common\Aws; is placed in initialize.php, below reference to Slim:

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

The rest of the code is in the controller:

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

I am still getting the following error:

Class 'UserFrosting\Aws' not found.

What am I missing?


回答1:


As you can see, it is looking in the UserFrosting\ namespace for the Aws class, but it clearly does not live in there!

You need the use Aws\Common\Aws; at the top of every file where you want to reference the class Aws. Alternatively, you can simply reference the class using its fully qualified name:

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

I would suggest taking an hour or so to learn more about PHP Namespaces. They are an extremely important concept in modern PHP, and are closely related to Composer, autoloading, and the PSR-4 standard.



来源:https://stackoverflow.com/questions/40595182/userfrosting-aws-sdk

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