0
votes

I'm trying to test a dummy component of my own with a Service Provider, but I'm getting this error:

FatalErrorException in ProviderRepository.php line 146: Class 'Luismartin\Notificador\Notificador' not found

This is the Notificador class, located in vendor/luismartin/notificador/src/Notificador.php (I ran a composer dump-autoload after creating it):

<?php
namespace Luismartin\Notificador;

use Illuminate\Foundation\Auth\User;
use Illuminate\Mail\Mailer;


class Notificador
{
    private $mailer;

    public function __construct(Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function notificar(User $usuario, $mensaje)
    {
        // notifier actions...
    }

}

Its Service Provider, placed in app/Providers/NotificadorServiceProvider.php:

<?php
namespace App\Providers;

use Illuminate\Mail\Mailer;
use Illuminate\Support\ServiceProvider;
use Luismartin\Notificador\Notificador;

class NotificadorServiceProvider extends ServiceProvider
{

    public function register()
    {
        $this->app->singleton('Notificador', function() {
            return new Notificador(new Mailer());
        });
    }

}

It has been added in config\app.php:

'providers' => [
     //...,
     Luismartin\Notificador\Notificador::class,
],

And finally, the controller method in which I'm trying to use the Notificador component:

private function notificar($mensaje)
{
        $admin = User::where('name', 'admin')->first();

        $notificador = App::make('Notificador');

        $notificador->notificar($admin, $mensaje);
}

What else should I do?

I've also tried deleting composer.lock and running composer update. Here's what it displays at the end:

Writing lock file
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
> php artisan optimize
PHP Fatal error:  Class 'Luismartin\Notificador\Notificador' not found in /home/vagrant/Code/helpdesk/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository
PHP Stack trace.....
1
Delete your composer.lock file and run composer update again. Also ry changing location in the app.php file to App/Providers/NotificadorServiceProvider::class (do this before composer update)Mike Barwick
How is this package being installed? Composer? If so, keep service provider in vendor folder with your other code.Mike Barwick
I've deleted composer.lock and run composer update. It also displays an error related to my class. I've added it above.Luis Martin
@MikeBarwick I wrote my package directly in the vendor directory. I didn't use any remote location.Luis Martin

1 Answers

0
votes

I found the error. I confused the component class by the Service Provider class when declaring it in the providers list:

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

Now it works.