imstuck81
imstuck81

Reputation: 21

Laravel 8 - Facade Class Not Found

I have been struggling with this after trying to replicate simple tutorials to get started a new project.

Folllowing the tutorials here: https://medium.com/@cmanish049/creating-custom-facades-in-laravel-b9b72d573752

  1. Created The Helper Function

     namespace App\CSVUtils;
    
      class CSVParser
      {
    
        public function sayHello()
        {
         echo "Hello, from Facade class.";
        }
      }
    
  2. Created The Service Provider

    <?php
    
    namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    
    use App\CSVUtils\CSVParser;
    
    class CsvParserServiceProvider extends ServiceProvider
    {
     /**
      * Register services.
      *
      * @return void
      */
     public function register()
     {
         $this->app->bind('csvparser',function(){
    
             return new CSVParser();
    
          });
     }
    
     /**
      * Bootstrap services.
      *
      * @return void
      */
     public function boot()
     {
         $this->app->bind('csvparser',function(){
    
             return new CSVParser();
    
          });
       }
     }
    
  3. Created the Facade

    <?
    namespace App\CSVUtils;
    
    use Illuminate\Support\Facades\Facade;
    
    class CSVParserFacade extends Facade
    {
      protected static function getFacadeAccessor()
      {
        return 'csvparser';
      }
    }
    
  4. Added the following to the app/config.php (I did not remove any of the default entries just added the following

    'providers' => [
         App\Providers\CsvParserServiceProvider::class,
         ...
       ],
    
     'aliases' => [
    
         'CSV' => App\CSVUtiles\CSVParserFacade::class,
    
         ...
     ]
    
  5. Called the Alias in the my controller

    <?php
    
      use Illuminate\Support\Facades\Route;
    
      Route::get('/csv', function () {
    
        CSV::sayHello();
    
       //return view('welcome');
      });
    

RESULT:

ErrorException Class "App\CSVUtiles\CSVParserFacade" not found http://localhost/csv

Illuminate\Foundation\Bootstrap\HandleExceptions::handleError vendor/laravel/framework/src/Illuminate/Foundation/AliasLoader.php:80

I have tried a bunch of things, renaming classes / files changing the namespace, calling the Facade directly (rather than from the alias)

I have also tried the following:

composer dump-autoload php artisan config:clear php artisan cache:clear php artisan clear-compiled

And now I feel like I'm loosing my mind. It's been a busy day and my brain is fried but I'm hoping I am missing something simple.

Please help!! :)

Upvotes: 2

Views: 3811

Answers (3)

user21145502
user21145502

Reputation: 11

This is a machine translation, so the text may be wrong. I had the same phenomenon and I will share the cause in my case.

To compare to the example, it was due to the fact that the directory should have been created with "CSVUtils", but it was whitespaced with "CSVUtils ".

Upvotes: 1

S N Sharma
S N Sharma

Reputation: 1526

Just a little spelling mistake. Correct the spelling in app.php aliases array.

Use this line of code

'CSV' => App\CSVUtils\CSVParserFacade::class,

enter image description here

Upvotes: 0

mk23
mk23

Reputation: 1403

Update below code - aliases for CSV :

'providers' => [
     App\Providers\CsvParserServiceProvider::class,
     ...
   ],

 'aliases' => [

     'CSV' => App\CSVUtils\CSVParserFacade::class,

     ...
 ]

Upvotes: 0

Related Questions