nWidart / laravel-modules

Module Management In Laravel
https://docs.laravelmodules.com
MIT License
5.56k stars 969 forks source link

How To Work With Subscriber? #1808

Closed raviMukti closed 6 months ago

raviMukti commented 7 months ago

Hi, I'm new to using Laravel Modules, and have tried Event-Driven (via Event-Listener) but there is a little problem, where when using the subscriber method, the listener doesn't listen to events and the event is not triggered, oh yes, coincidentally the storage for the queue uses a database, is it does that also matter?

VerifiedAccountSubscriber.php

class VerifiedAccountSubscriber
{
    use Dispatchable;

    public $queue = "account";

    public function onVerifiedAccount(AccountVerified $event)
    {
        DB::transaction(function () use ($event)
        {
            /** @var \Modules\Merchant\Entities\Merchant $merchant */
            $merchant = MerchantRepository::create([
                "name" => "Merchant " . $event->user->id,
                "code" => uniqid("MERCHANT")
            ]);

            /** @var \Modules\Merchant\Entities\Location $location */
            $location = LocationRepository::create([
                "name" => "Default",
                "code" => uniqid("LOCATION"),
                "full_address" => "Default",
                "is_primary" => true
            ]);

            UserMerchantRepository::create([
                "user_id" => $event->user->id,
                "merchant_id" => $merchant->merchant_id,
                "is_primary" => true
            ]);

            MerchantLocationRepository::create([
                "merchant_id" => $merchant->merchant_id,
                "location_id" => $location->location_id
            ]);
        });
    }

    public function subscribe(Dispatcher $events)
    {
        $events->listen(
            '\Modules\Auth\Events\AccountVerified',
            '\Modules\Merchant\Listeners\VerifiedAccountSubscriber@onVerifiedAccount'
        );
    }
}

Merchant Event Service Provider

<?php

namespace Modules\Merchant\Providers;

use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return [];
    }

    protected $subscribe = [
        '\Modules\Merchant\Listeners\VerifiedAccountSubscriber'
    ];
}

MerchantServiceProvider.php image

raviMukti commented 6 months ago

I will consider this issue as closed, I answered my own question after finding the bugs in this library. You can check my answer in the following stackoverflow question here