phpamazon-web-servicescodeigniter-4

AWS namespace not being loaded - Codeigniter 4.6, PHP 8.3


I am trying to connect to an AWS Connect instance to read Cases and the associated Contact records. I am using a simple controller at first just to get the connections set up correctly and the API calls. I am using CodeIgniter 4.6 running on a Wampserver using PHP 8.3.

It looks like the basic set up is correct, if I just run the simple default controller I get the CodeIgniter welcome. I also used Composer to install the AWS PHP SDK-V3.

Here is the code I have to set up the AWS namespaces and call the appropriate AWS API methods in the SDK:

require '../vendor/autoload.php';

use Aws\ConnectCases\ConnectCasesClient;
use Aws\Exception\AwsException;
use Aws\Common\Credentials\Credentials;

class Home extends BaseController
{
    public function index(): string
    {   
        echo ('does it exist?: <br />'); 
        var_dump(class_exists('AWS\ConnectCases\ConnectCasesClient'));
        exit;
        $ccClient = new ConnectCasesClient([
            'region'  => 'us-east-1',
            'version' => 'latest',
            'key'     => 'xxxxxxxxx',
            'secret'  => 'xxxxxxxxx',
            'token'   => 'xxxxxxxxx'
        ]);

class_exists reports False which indicates to me that the AWS namespace is not being setup or found properly. If I remove the exit; I get an Http 500 Server error. I am sure this is some stupid configuration blunder, can anyone point me in the right direction?

Additional info: Composer json:

{
    "require": {
        "codeigniter4/framework": "^4.6",
        "aws/aws-sdk-php": "^3.337"
    }
}

And here is the directory listing of the vendor folder:

enter image description here


Solution

  • There are multiple issues which needs to be fixed.

    1. In var_dump, change AWS to Aws
    2. credentials key is not used in the array. See the below updated code
    3. In CodeIgniter 4 autoload is included by default, just confirm whether autoloader is properly set up via vendor/autoload.php

    Here is the updated script:

    use Aws\ConnectCases\ConnectCasesClient;
    use Aws\Exception\AwsException;
    
    class Home extends BaseController
    {
        public function index(): string
        {   
            // Verify class existence with correct namespace casing
            var_dump(class_exists('Aws\ConnectCases\ConnectCasesClient'));
    
            $ccClient = new ConnectCasesClient([
                'region'      => 'us-east-1',
                'version'     => 'latest',
                'credentials' => [
                    'key'    => 'xxxxxxxxx',
                    'secret' => 'xxxxxxxxx',
                    // 'token' => '...' // Include only if using session tokens
                ]
            ]);
    
            // Rest of your logic
        }
    }
    

    Also run the below composer command to regenerate autoload mapping:

    composer dump-autoload