DI Container unable to find Autoloaded Class


#1

Hey There,

I have a Slim application that is working as expected in my localhost environment, however after uploading it to the production server I keep getting a fatal error.

The basic app structure is:

-- includes
-- | -- config
-- | -- src
-- | -- | -- controller
-- | -- | -- | -- InputController.php
-- | -- | -- routes
-- | -- | -- | -- input.php
-- | -- | -- dependencyContainer.php
-- | -- vendor
-- | -- composer.json
-- | -- composer.lock

With the index and .htaccess on the public part of the server:

-- public
-- | -- index.php
-- | -- .htaccess

index.php

<?php

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require 'path_to_includes_folders/vendor/autoload.php';

$config = parse_ini_file('path_to_includes_folders/config/config.ini', true);


$app = new \Slim\App(["settings" => $config]);

// Set up dependencies
require 'path_to_includes_folders/src/dependencyContainer.php';

$app->get('/test/{name}', function(Request $request, Response $response, $args) {
    echo 'Hello, ' . $args['name'];
});

require 'path_to_includes_folders/src/routes/input.php';

$app->run();

routes/input.php

<?php

$app->get('/initial/{name}/{age}','InputController:initialise');

dependencyContainer.php

$container = $app->getContainer();
$container['InputController'] = function ($container) {
    return new App\Controller\InputController($container['InputView'], $container['InputService']);
};

composer.json

{
    "require": {
        "slim/slim": "^3.0",
        "slim/php-view": "^2.2"
    },
    
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    }
}

When i send index.php/test/bob - it works as expected.

However, when I send index.php/initial/bob/20 I get a “PHP Fatal error: Class ‘App\Controller\InputController’ not found in /full_path_to_includes/src/dependencyContainer.php”

I’ve tried doing an update and dump-autoload with composer on the localhost files (as I can’t on the server), however it hasn’t helped.

At the momen I’m stuck for ideas on what could be causing it - I’d be really grateful for any suggestions :slight_smile:


#2

You haven’t shown the content of the controller itself, so it’s difficult to be absolutely sure, but as long as it’s correctly namespaced, I would guess that the problem is that the controller directory starts with a lower-case C, whereas the namespace that you’re trying to load in your DIC has an upper-case C. They need to match, especially if you’re using a case-sensitive file system (which usually just means not Windows)


#3

Thank you so much - I completely forgot about that when moving it from windows. I can enjoy the weekend now! :slight_smile: