Reputation: 1589
I was watching this lesson and was trying to figure out which directory to put the EmailNotifier
class file since it's an Event
.
I don't know if it belongs in App\Events
or App\Handlers\Events
.
This is what I currently have:
<?php namespace App\Mailers;
use Illuminate\Mail\Mailer as Mail;
abstract class Mailer {
private $mail;
function __construct(Mail $mail)
{
$this->mail = $mail;
}
public function sendTo($user, $subject, $view, $data)
{
$this->mail->queue($view, $data, function ($message) use ($user, $subject)
{
$message->to($user->email)->subject($subject);
});
}
}
<?php namespace App\Mailers;
use App\User;
class UserMailer extends Mailer {
/**
* @param User $user
*/
public function sendWelcomeMessageTo(User $user)
{
$subject = 'Welcome To Backstage!';
$view = 'emails.registeration.confirm';
$data = [];
return $this->sendTo($user, $subject, $view, $data);
}
}
<?php namespace App\Handlers\Events;
class EmailNotifier extends Event {
private $mailer;
public function __construct(UserMailer $mailer)
{
$this->mailer = $mailer;
}
public function whenUserHasRegistered(UserHasRegistered $event)
{
$this->mailer->sendWelcomeMessageTo($event->user);
}
}
<?php namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
class UserHasRegistered extends Event {
use SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
{
//
}
}
Upvotes: 0
Views: 134
Reputation: 5700
That's more of a discretionary concern. You generally want to categorize similar purpose items into the same namespace. Handlers\Events
sounds like a place to put event handlers maybe, or perhaps it's a place for events that stem from handlers.
It sounds like you are on the right track placing an event in an Events
namespace. Convention and consistency is the key. It doesn't matter so much as to what the final namespace is, just as long as it is consistent. IMO a more logical approach would be to have App\Event
for all of your events and potentially sub namespace it from there for event categories. Handlers would be more self explanatory if they were somewhere like App\EventHandler
and again sub namespaced into groups as needed.
That way it is pretty clear to an outsider who may need to work with your code in the future. That's my two cents as far as a general organization structure goes.
With deeper context into Laravel as the link laracasts.com implies. The App\Event
namespace is for events which is what your EmailNotifier
looks to be, where App\Handlers\Events
is generally for handlers, subscribers, listeners, whatever you want to call them.
Upvotes: 1