2

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?

gvasquez
  • 1,919
  • 5
  • 27
  • 41
Luke G
  • 1,741
  • 6
  • 23
  • 34

1 Answers1

2

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.

alexw
  • 8,468
  • 6
  • 54
  • 86
  • 1
    To add to this, there is no way to address the OP's specific error without seeing more code context to understand where/how the `UserFrosting` namespace is invoked, but this answer should get the OP pointing in the right direction. – Mike Brant Nov 14 '16 at 22:15
  • @MikeBrant Most [controller files](https://github.com/userfrosting/UserFrosting/blob/master/userfrosting/controllers/AccountController.php) in this version of UserFrosting start with `namespace UserFrosting;` at the top. So if OP isn't familiar with namespaces, they probably don't realize that PHP will consider this the namespace for ALL references to classes unless explicitly declared otherwise. – alexw Nov 15 '16 at 16:57