I am currently working on building an API in the Slim framework, and having a bit of trouble with it not finding classes that should be getting autoloaded.
Folder structure
database
- db_connection.php
<?php
namespace Database;
class DBConnection {}
...
public
- index.php
src
- middlewares
-- authentication_middleware.php
<?php
namespace Middleware;
class AuthenticationMiddleware {}
...
- routes
-- different routes for tickets, projects, notifications etc.
- utils
-- data_convertor.php
<?php
namespace Utils;
class DataConvertor {}
...
And here is my composer.json:
{
"autoload": {
"psr-4": {
"Database\\": "./database",
"Middleware\\": "./src/middlewares",
"Utils\\": "./src/utils"
}
},
"require": {
// Dependencies
}
}
Now if I go into my projects.php file in src/routes and use Database\DBConnection, then do $db_connection = new DBConnection(); further down, all is well, my database connects and the app returns a response. However, now that I have attempted to create these other namespaces, Middleware and Utils, I am suddenly getting "Uncaught Error: Class "Middleware\AuthenticationMiddleware" not found". It is in Slim 4 so here is what it ultimately looks like:
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Database\DBConnection;
use Middleware\AuthenticationMiddleware;
$app->group('/projects', function($app) {
$app->get('', function(Request $request, Response $response, Array $args) {
try {
$db_connection = new DBConnection();
// The rest of the code
return $response->withStatus($status);
});
})->add(new AuthenticationMiddleware()); // Here is where the "error" is generated.
What on Earth could I be doing wrong? I have gone with the theory that Middleware might be a protected word so changed it to something else, to no avail, and even ended up renaming Middleware to MW and AuthenticationMiddleware to AM to eliminate any chance of mispelling, again to no avail. Between every step I have also been running composer update and even composer dumpautoload && composer update to try and get it working.
As the title may suggest, the one thing the non-loading ones have in common, and separate from the loading one, is that database is a top-level directory and the others aren't. But surely as long as the path is correct in composer.json that shouldn't matter?
Further to this, a bit more Googling before posting has led me to look at vendor/composer/autoload_psr4.php and sure enough, it returns the below:
return array(
'Utils\\' => array($baseDir . '/src/utils'),
'Middleware\\' => array($baseDir . '/src/middlewares'),
'Database\\' => array($baseDir . '/database'),
// Plenty of auto-generated ones with plenty of slashes in the directory paths
)
Thanks to the comments on my question I now know that the issue was that the class name e.g.
AuthenticationMiddlewaredidn't match the filename, in this caseauthentication_middleware.php, changing that by renaming the files has fixed the issue. Weird that the database connection was still working despite that - but hey, it now all works so I'm good!