Why App\Modules\ServiceProvider Is Not Found in Your Laravel Project
You are encountering a Class 'App\Modules\ServiceProvider' not found error because you have defined an abstract base class but failed to instantiate it properly within your autoload configuration. While your directory structure and composer.json settings might look correct at a glance, the core issue lies in how Composer handles abstract classes versus concrete implementations during autoloading.
The Abstract Class Trap
In your app/modules/ServiceProvider.php, you defined an abstract class:
abstract class ServiceProvider extends \Illuminate\Support\ServiceProvider {
// ...
}
Abstract classes cannot be instantiated directly. They serve as blueprints for other classes to inherit from. When Laravel attempts to load your project, it scans the app/modules directory (as per your classmap entry) and finds this abstract class. However, if your application logic or a specific bootstrapping process tries to instantiate App\Modules\ServiceProvider directlyâperhaps because of how the parent namespace is referencedâit will fail immediately.
More critically, Composer's class map autoloader only includes files that are explicitly mapped or can be resolved as concrete classes. If your main application provider array in config/app.php references App\Modules\Points\ServiceProvider, Laravel relies on the autoloader to find it. The autoloader successfully finds the child class because it is concrete, but the error suggests a mismatch in how the parent is being referenced or loaded relative to the child.
Autoload Configuration and Class Mapping
You added the following to your composer.json:
"autoload": {
"classmap": [
"app/modules"
]
},
And ran composer dump-autoload. This is generally correct for organizing code within a specific directory. However, ensure that this entry sits alongside the standard PSR-4 mappings in your root composer.json. If you are using PSR-4 (which is default in modern Laravel), you should also define a namespace mapping.
If your project relies on App\Modules\ServiceProvider being found as a standalone entity before the child classes, you must ensure that abstract classes do not interfere with the loading order. A better approach often involves ensuring that only concrete Service Providers are registered in the providers array of config/app.php.
Correcting the Implementation
To resolve this, verify that you are not attempting to instantiate the abstract base class anywhere in your codebase. Instead, ensure that the concrete implementations extend the base class correctly and that the base class itself is not referenced directly in dependency injection or service registration arrays.
Here is how you should structure the autoload section if you want to stick with classmap for this specific folder:
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"app/modules"
]
}
This ensures that Laravel's standard namespace mapping handles the rest of the application, while your app/modules directory is explicitly included for any non-standard paths. After updating composer.json, always run:
composer dump-autoload --optimize
Optimizing the autoload files can sometimes resolve issues where classes are not being detected efficiently during the boot process.
Registering Providers Correctly
In your config/app.php, you registered:
'providers' => [
'App\Modules\Points\ServiceProvider',
],
This is valid, provided that App\Modules\Points\ServiceProvider can successfully instantiate its parent. The error message implies that somewhere in the chain, the system expects App\Modules\ServiceProvider to exist as a loadable class. If your child class extends \App\Modules\ServiceProvider, the autoloader must have already loaded the parent before loading the child.
If you are using an older version of Laravel or a specific custom setup where the base provider is expected to be loaded globally, consider moving the abstract class to a location that does not conflict with the main application namespace, or simply remove the abstract base class if it is not strictly necessary for your modular architecture.
Final Steps
If you continue to face issues, double-check that no other file in your app/modules directory is trying to new App\Modules\ServiceProvider(). Additionally, ensure that your config/app.php providers array does not list the abstract class directly. By registering only the concrete child providers and ensuring the autoloader maps both PSR-4 and classmap entries correctly, the "Class not found" error should resolve itself.