Slim框架依赖报错:无法找到MainController类
That extra Slim\ prefix in your error message is exactly what's throwing things off. Your controller lives under the App\Controllers namespace (as set in your composer.json and the controller file itself), but your app is incorrectly trying to look for it under Slim\App\Controllers. Let's walk through the fixes step by step:
1. Check Your Route Configuration (routes.php)
This is the most likely culprit. If your route references the controller with an incorrect namespace that includes Slim\, that's where the error originates.
What you might have (wrong):
$app->get('/search', '\Slim\App\Controllers\MainController:search');
What it should be (correct):
$app->get('/search', '\App\Controllers\MainController:search');
Or even cleaner, use the container alias you already set up:
$app->get('/search', 'MainController:search');
2. Verify Your Dependencies Container Setup
Looking at your dependencies.php, your MainController container definition is correct (new App\Controllers\MainController), but just double-check there's no accidental Slim\ prefix slipped in anywhere here. Also, since your index.php includes this file after initializing $app, the variable should be properly available—no issues there.
3. Refresh Composer's Autoloader
Since you're using PSR-4 autoloading, make sure Composer has generated the correct autoload mappings. Run this command in your project root:
composer dump-autoload
This ensures PHP can correctly find your App\Controllers\MainController class by mapping the App\ namespace to your src/ directory.
4. Double-Check the Controller's Namespace
Your /src/Controllers/MainController.php already has the correct namespace namespace App\Controllers;, so that's solid—just make sure you didn't accidentally tweak this to include Slim\ at any point.
Once you address these areas, that "class not found" error should vanish, and your controller will load as expected.
内容的提问来源于stack exchange,提问作者The Hawk

