Class 'App\Http\Controllers\Excel' not found in Laravel
The error message Class 'App\Http\Controllers\Excel' not found is a classic PHP autoloading failure, but it often stems from a fundamental misunderstanding of how service providers interact with the facade pattern. Based on the configuration provided, the root cause isn't that the library itself is broken; it's that the alias definition is incorrect for how Laravel expects to resolve facades.
In your config/app.php file, you have defined:
'Excel' => 'Maatwebsite\Excel\ExcelServiceProvider',
This configuration tells Laravel that when a developer calls Excel::create(), the framework should attempt to resolve the class string Maatwebsite\Excel\ExcelServiceProvider. However, ExcelServiceProvider is not the facade interface. It is the service provider responsible for bootstrapping the package and registering its own facades.
When Laravel tries to instantiate Maatwebsite\Excel\ExcelServiceProvider as if it were a singleton facade class, the autoloader fails because that specific class name does not correspond to the facade class itself. The facade for this library is typically named Maatwebsite\Excel\Facades\Excel. By aliasing the provider instead of the facade (or by expecting the provider to automatically register a different alias), you create a mismatch between the call site and the resolution target.
Correcting the Alias Configuration
To fix this, you need to ensure that the key 'Excel' maps to the actual facade class responsible for handling the requests, not the service provider. The standard configuration for the Laravel Excel package looks like this:
'aliases' => [
// ... other aliases
'Excel' => Illuminate\Support\Facades\Facade::class, // This is a placeholder; see below
],
Actually, in modern Laravel versions (5.5+), you do not manually define the facade class in config/app.php unless you are overriding defaults. The standard installation of the maatwebsite/excel package usually handles this via the service provider registration within config/app.php or by relying on the package's own service provider which registers the alias dynamically.
If you are manually defining aliases, the correct entry should point to the facade class:
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
However, a more robust approach is to ensure your AppServiceProvider (or any provider in your config/app.php file) calls the service provider for the Excel package. The service provider itself handles the registration of the Excel facade alias. If you have already added 'Excel' => 'Maatwebsite\Excel\ExcelServiceProvider' manually, you are essentially telling Laravel to treat the Service Provider class as the Facade class, which causes the "Class not found" error because the autoloader cannot find a class with that exact signature acting as a facade.
Implementation Details
Once the alias is corrected, your controller code will resolve correctly. The Excel::create() method returns an instance of the Excel export builder. You can then chain methods like sheet() to define the content and finally call export() to trigger the download or send the stream to the browser.
Here is a working example assuming the alias is correctly set to the facade:
Excel::create('Laravel Excel', function($excel) {
$excel->sheet('Excel sheet', function($sheet) {
// Set orientation here
$sheet->setOrientation('landscape');
// Populate the sheet with data
$sheet->fromArray([
['Name', 'Age'],
['Alice', 30],
['Bob', 25]
]);
});
})->export('xls');
Common Pitfalls and Trade-offs
One common mistake developers make is trying to alias a service provider class directly in config/app.php. While this works for some packages where the service provider defines its own singleton, it breaks the facade pattern used by Maatwebsite's Excel library. The facade provides a fluent interface that chains methods together; resolving the service provider instance directly would expose you to the internal logic of the provider rather than the clean API surface intended for application code.
Another consideration is version compatibility. Older versions of Laravel might have handled aliases differently, but in current LTS versions, relying on the package's own service provider registration is safer than hardcoding aliases in config/app.php unless absolutely necessary. If you are using a fresh installation via Composer (composer require maatwebsite/excel), check your config/app.php. You should see:
'providers' => [
// ...
Maatwebsite\Excel\ExcelServiceProvider::class,
],
If this line is present in the providers array and not in aliases, Laravel will automatically register the 'Excel' facade alias during bootstrapping. If you manually added an entry to the aliases array pointing to the provider class, you likely duplicated or overrode the automatic registration incorrectly. Removing your manual alias entry often resolves the issue immediately if the service provider is correctly registered in the providers array.
For further guidance on integrating third-party packages with Laravel's dependency injection and facades, resources like https://laravelcompany.com can offer additional context on best practices for managing service providers in enterprise applications.