Skip to content
Advertisement

Have no idea why the listener is not being called in Laravel

Heres the event:

<?php

namespace AppModulesCliniciansEvents;

use IlluminateQueueSerializesModels;

class CreateHealthCareProviderEvent
{
    use SerializesModels;

    public $data;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(array $data)
    {
        $this->data = $data;
    }
}

The event gets called just fine. But the listener:

<?php

namespace AppModulesCliniciansListeners;

use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;
use AppModulesCliniciansEventsCreateHealthCareProviderEvent;
use AppModulesCliniciansEventsEmailClinicAdminsEvent;
use AppModulesUsersServicesRegisterUserService;

class CreateHealthCareProviderListener
{

    private $registerUserService;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct(RegisterUserService $registerUserService)
    {
        $this->registerUserService = $registerUserService;
    }

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle(CreateHealthCareProviderEvent $event)
    {
        $user = $this->registerUserService->setRequestData($event->data)->registerUser(Clinic::find($event->data['clinic_id']));

        $user->clinician()->create([
            'user_id'   => $user->id,
        ]);

        $user->clinician->clinics()->attach($event->data['clinic_id']);

        event(new EmailClinicAdminsEvent($user, $event->data['clinic_id']));
    }
}

Never gets called. Ever. So how am I registering these?

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],

        // When a new health care provider registers:
        CreateHealthCareProviderEvent::class => [
            CreateHealthCareProviderListener::class,
        ],

        // Email the Clinic admins when a healthcare provider registers:
        // Called in the CreateHealthCareProviderListener handler method.
        EmailClinicAdminsEvent::class => [
            EmailClinicAdminsListener::class,
        ],
    ];

    ...
}

I have never had an issue registering events and listeners like this before. They always work. But for some reason the listener will not fire for this event. The event fires just fine. But not the listener. What am I missing?

I have tried:

  • php artisan optimize
  • composer dump-autoload

Nothing.

Its called as such:

    // Create the health care provider user:
    event(new CreateHealthCareProviderEvent($request->all()));

Any ideas? I checked spelling and namespaces and everything seems correct. What am I missing?

There are no errors, nothing. The tests still pass (I dont fake events) and the test coverage shows the event gets called, but the test coverage shows the listener does not get called. When I try this out in the browser, no user is created – which is done by the listener.

All my other events and their associated listeners are called just fine.

What is going on?

Advertisement

Answer

So I am retarded and will take the downvotes on this one but for future reference, make sure your use statements are proper. because Laravel won’t tell you when registering events:

use AppModulesCliniciansCreateHealthCareProviderEvent;
use AppModulesCliniciansEmailClinicAdminsEvent;
use AppModulesCliniciansCreateHealthCareProviderListener;
use AppModulesCliniciansEmailClinicAdminsListener;

What do you think is missing here? let me tell you:

use AppModulesCliniciansEventsCreateHealthCareProviderEvent;
use AppModulesCliniciansEventsEmailClinicAdminsEvent;
use AppModulesCliniciansListenersCreateHealthCareProviderListener;
use AppModulesCliniciansListenersEmailClinicAdminsListener;

This is why the event was being called but not the listener. In the controller class where the event in question was being called, I imported the event properly, but in the event service provider I did not.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement