Autoload is loading, but still getting class is not resolvable

Hi guys!

I have just created a new Slim 4 app using the slim/slim-skeleton and added a new folder under /src/Application/ named Controllers and put 2 new controllers in it:

  • WelcomeController
  • DemoController

And then used both composer dump-autoload and composer dumpautoload -o and I did see the 2 new classes generated in the autoload cli (the number increased) and also I opened the autoload_classmap.php file and saw they both show in the array.

The issue is, when I’m trying to use these classes, I am getting this response:

{
    "statusCode": 500,
    "error": {
        "type": "SERVER_ERROR",
        "description": "App\\Application\\Controllers\\DemoController is not resolvable"
    }
}

This is the WelcomeController class (the DemoController is similar):

<?php
declare(strict_types=1);

namespace App\Application\Controllers;

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

class WelcomeController {

    public function _construct() {

    }

    public function welcome(Request $request, Response $response) {
        $response->getBody()->write('Welcome World! 🙂');
        return $response;
    }
}

And this is how I’m using it in my routes:

<?php
declare(strict_types=1);

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

// App
use Slim\App;
use Slim\Interfaces\RouteCollectorProxyInterface as Group;

// Controllers
use App\Application\Controllers\WelcomeController;
use App\Application\Controllers\DemoController;

return function (App $app) {
    $app->options('/{routes:.*}', function (Request $request, Response $response) {
        // CORS Pre-Flight OPTIONS Request Handler
        return $response;
    });

    $app->get('/welcome', WelcomeController::class, 'welcome');
    $app->get('/demo', DemoController::class, 'demo');
};

This is how they both show up in autoload_classmap.php:

‘App\Application\Controllers\DemoController’ => $baseDir . ‘/src/Application/Controllers/DemoController.php’,
‘App\Application\Controllers\WelcomeController’ => $baseDir . ‘/src/Application/Controllers/WelcomeController.php’,

What could be the issue?
Thanks!

Try replacing:

with

or

both work for me.

1 Like

Thanks @darkalchemy! It worked :slight_smile:
It’s really weird that this syntax is not mentioned in the docs.

1 Like

I think I found it in the php-di docs, but I’m not entirely certain of that.