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
)
My Answer:
It looks like your namespace declarations in your classes are not matching the namespace paths you have defined in your composer.json file.
In your AuthenticationMiddleware class, you have declared the namespace as `namespace Middleware;`, but in your composer.json file, you have defined the namespace path as `"Middleware\\": "./src/middlewares"`.
To fix this issue, you need to make sure that the namespace declarations in your classes match the namespace paths you have defined in your composer.json file.
For example, in your AuthenticationMiddleware class, you should declare the namespace as `namespace Middleware;` and in your composer.json file, you should define the namespace path as `"Middleware\\": "./src/middlewares"`.
Once you have made sure that the namespace declarations in your classes match the namespace paths you have defined in your composer.json file, run `composer dumpautoload` to regenerate the autoloader files and see if that resolves the issue.
If you continue to experience issues, double-check your namespace declarations and paths to ensure they are consistent and correct.
Rate this post
3 of 5 based on 7700 votesComments