I have read many posts on similar problem… nothing works.
1/ I installed the package with composer.
composer require barryvdh/laravel-dompdf
2/ Created app/Http/Controllers/PdfController.php
namespace AppHttpControllers;
use PDF;
use IlluminateHttpRequest;
class PdfController extends Controller
{
public function index()
{
$pdf = PDF::loadView('pdf.sample', [
'title' => 'CodeAndDeploy.com Laravel Pdf Tutorial',
'description' => 'This is an example Laravel pdf tutorial.',
'footer' => 'by <a href="https://codeanddeploy.com">codeanddeploy.com</a>'
]);
return $pdf->download('sample.pdf');
}
}
3/ added the route in web.php (the syntax works for my other routes)
// pdf
//Route::get('/pdf', [PdfController::class, 'index']); (originally)
Route::get('/pdf', 'AppHttpControllersPdfController@index')->name('pdf');
4/ cleaned as much as possible
php artisan cache:clear
php artisan optimize
php artisan route:clear
5/ if I run xxx/pdf or php artisan route:list, I get the error
Target class [AppHttpControllersPdfController] does not exist.
2
Answers
Sometimes strange things happen. My Mac was not saving files... so although I could see the controller as saved in VSCode... it was not saved on disk... so weird! After rebooting, everything worked fine.
php artisan optimize
is actually for caching, rather than just clearing – I wouldn’t recommend using it during development.The above deal with views, routes and cache data, but you likely need a way to clear your cached php class list (as
PdfController
is not being detected, but the file exists).dumpautoload @ composer docs