skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  2. php artisan optimize is actually for caching, rather than just clearing – I wouldn’t recommend using it during development.

    php artisan cache:clear // Flushes application cache
    php artisan route:clear // Clears compiled routes
    php artisan view:clear // Clears compiled views
    

    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).

    composer dumpautoload // Regenerates list of autoloaded classes
    

    dumpautoload @ composer docs

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search