Laravel 5.3 Class Not found
shadywattay opened this issue · 5 comments
After updating composer with required packages and registering in config/app.php, I've tried laravel-fpdf in laravel 5.3,
composer.json
"require": {
.....
"codedge/laravel-fpdf": "^1.0"
},
providers
Codedge\Fpdf\FpdfServiceProvider::class,
aliases
'Fpdf' => Codedge\Fpdf\Facades\Fpdf::class,
simply in web.php routes like
Route::get('GeneratePdf', function (Codedge\Fpdf\Fpdf\FPDF $fpdf) {
$fpdf->AddPage();
$fpdf->SetFont('Courier', 'B', 18);
$fpdf->Cell(50, 25, 'Hello World!');
$fpdf->Output();
});
And I'm getting error
ReflectionException in Route.php line 339:
Class Codedge\Fpdf\Fpdf\FPDF does not exist
What could possibly be wrong ?
Sorry for the late answer!
Did you try regenerating your Composer autoload classes with composer dump-autoload
?
I tried with Laravel 5.3 both ways:
- Separate Controller
- Definition in
routes/web.php
as you did
Works flawlessly!
May I suggest where the source of the problem is?
src/Fpdf/fpdf.php
file defines a class named FPDF
and the service provider searches for the correct class name FPDF
all uppercase. However, it seems the psr-4 autoloader expects file name to match class name in case-sensitive fashion:
http://www.php-fig.org/psr/psr-4/
I have this failing with laravel 4.2 project with same error of Class not found
. Renaming fpdf.php
to FPDF.php
fixes the problem. Not sure why it works sometimes but it seems file name must be fixed...
Thanks @kroky for checking. I'll try to investigate and will fix if I can reliably reproduce it. A better naming could indeed be a solution.
@kroky Thank you very much kroky, i'm also facing this problem. Your solution perfectly solved it , guess psr-4 couldn't find the correct file with difference on upper/lower case. As a class has to be start with Upper case , i just renamed filename as well, everything working perfectly.
cheers
make sure you rename the file fpdf.php in src/Fpdf so that it start with a capital letter