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 ModulesMerchantEntitiesMerchant $merchant */
$merchant = MerchantRepository::create([
"name" => "Merchant " . $event->user->id,
"code" => uniqid("MERCHANT")
]);
/** @var ModulesMerchantEntitiesLocation $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(
'ModulesAuthEventsAccountVerified',
'ModulesMerchantListenersVerifiedAccountSubscriber@onVerifiedAccount'
);
}
}
Merchant Event Service Provider
<?php
namespace ModulesMerchantProviders;
use IlluminateFoundationSupportProvidersEventServiceProvider 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 = [
'ModulesMerchantListenersVerifiedAccountSubscriber'
];
}
VerifyController.php
public function verify(Request $request)
{
try
{
$userId = $request->query("user");
$user = UserRepository::where([
["id", "=", $userId],
])->first();
if(! URL::hasValidSignature($request))
throw new BadRequestException(AuthError::VERIFICATION_URL_INVALID["message"], AuthError::VERIFICATION_URL_INVALID["code"]);
if($user->hasVerifiedEmail())
throw new BadRequestException(AuthError::EMAIL_ALREADY_VERIFIED["message"], AuthError::EMAIL_ALREADY_VERIFIED["code"]);
$user->markEmailAsVerified();
AccountVerified::dispatch($user);
$apiResponse = (new ApiResponseBuilder())
->setStatus(Response::HTTP_OK)
->setMessage(Response::$statusTexts[Response::HTTP_OK])
->build();
return response()->json($apiResponse, Response::HTTP_OK);
}
catch (Throwable $th)
{
throw $th;
}
}
AccountVerified.php
<?php
namespace ModulesAuthEvents;
use IlluminateBroadcastingInteractsWithSockets;
use IlluminateFoundationEventsDispatchable;
use IlluminateQueueSerializesModels;
use ModulesAuthEntitiesUser;
class AccountVerified
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return [];
}
public function queue()
{
return "account";
}
}
2
Answers
After almost 1 month, and making me frustration, it turned out that the command "php artisan module:make-provider" class generated was causing problems, the weird behaviour in the nWidart/modules, the library gave me the boilerplate of EventServiceProvider class like this.
somehow that's code wont work and never ever be scanned by laravel, linting not helps. its show me the code is no error. But when i try to scan the list of listener, it's just shows me this list.
After i change the class EventServiceProvider to remove the register() and the provides() method
Now the listener is being scanned
Now Its Worked!!!!
I can see multiple issues here:
Your
VerifiedAccountSubscriber
class needs to implement theIlluminateContractsQueueShouldQueue
interface.No need to use the
IlluminateFoundationEventsDispatchable
trait inside a listener class. This trait is used within events or jobs, but not within a listener.I also suspect that you may not be running the queue, so try to look inside the
jobs
and thefailed_jobs
table inside your database (since you’re using the database driver for the queue). If you find entries inside thejobs
table, it means that you need to start the queue worker using the command: