Luke G Luke G - 18 days ago 9
PHP Question

UserFrosting & AWS SDK

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?

Answer

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.

Comments