我正在开发一个 laravel 10 项目,我有几个类来处理电子邮件。 最近我添加了一些新的电子邮件通知。 我将通知代码分解为服务:
<?php
namespace App\Services;
use App\Models\User;
use App\Models\Comment;
use App\Models\Request;
use App\Mail\CommentMail;
use App\Mail\InvitationMail;
use App\Mail\NewRequestMail;
use App\Mail\AffectationMail;
use App\Mail\FinishedRequestMail;
use App\Mail\ApplicantAffectationMail;
use App\Megaphone\NewComment;
use App\Megaphone\NewRequest;
use App\Megaphone\NewInvitation;
use App\Megaphone\NewAffectation;
use App\Megaphone\FinishedRequest;
use App\Megaphone\ApplicantAffectation;
use Illuminate\Support\Facades\Mail;
class NotificationService
{
/**
* Sending notifications for new request.
*
* @param User $user
* @param Request $request
* @param bool $manager
* @return void
*/
public function sendNewRequestNotification(User $user, Request $request, $manager = false) : void
{
$notification = new NewRequest(
'Nouvelle demande',
$request->user->name . ' a créé une nouvelle demande : "' . $request->title . '".',
route('show', ['id' => $request->id]),
);
$this->sendMegaphoneNotification($user, $notification);
// Manager case => verify the right preference between isManagerEmailEnabled() and isMailEnabled().
if($manager) {
if($user->isManagerEmailEnabled()) {
$this->sendEmailNotification($user, $request, $notification);
}
} else {
if($user->isMailEnabled()) {
$this->sendEmailNotification($user, $request, $notification);
}
}
}
/**
* Sending notifications for new comment.
*
* @param User $user
* @param Request $request
* @param Comment $comment
* @return void
*/
public function sendCommentNotification(User $user, Request $request, Comment $comment) : void
{
$notification = new NewComment(
'Nouveau commentaire',
$comment->user->name . ' a laissé un nouveau commentaire à la demande "' . $request->title .'."',
route('show', ['id' => $request->id]),
);
$this->sendMegaphoneNotification($user, $notification);
if($user->isMailEnabled()) {
$this->sendEmailNotification($user, $request, $notification, $comment);
}
}
/**
* Sending notifications for new affectation.
*
* @param User $user
* @param Request $request
* @return void
*/
public function sendAffectationNotification(User $user, Request $request) : void
{
$notification = new NewAffectation(
'Nouvelle affectation',
'Vous avez reçu une nouvelle affectation pour la demande : "' . $request->title . '".',
route('show', ['id' => $request->id]),
);
$this->sendMegaphoneNotification($user, $notification);
if($user->isMailEnabled()) {
$this->sendEmailNotification($user, $request, $notification);
}
}
/**
* Sending notifications for new invitation.
*
* @param User $user
* @param Request $request
* @param string $senderName
* @return void
*/
public function sendInvitationNotification(User $user, Request $request, $senderName) : void
{
$notification = new NewInvitation(
'Nouvelle invitation',
$senderName . ' vous a envoyé une invitation à suivre la demande : "' . $request->title . '".',
route('show', ['id' => $request->id]),
);
$this->sendMegaphoneNotification($user, $notification);
if($user->isMailEnabled()) {
$this->sendEmailNotification($user, $request, $notification, null, $senderName);
}
}
/**
* Sending notifications for finished request.
*
* @param User $user
* @param Request $request
* @return void
*/
public function sendFinishedRequestNotification(User $user, Request $request) : void
{
$notification = new FinishedRequest(
'Demande terminée',
'La demande : "' . $request->title . '" est terminée.',
route('show', ['id' => $request->id]),
);
$this->sendMegaphoneNotification($user, $notification);
if($user->isMailEnabled()) {
$this->sendEmailNotification($user, $request, $notification);
}
}
/**
* Sending notifications for applicant affectation.
*
* @param User $user
* @param Request $request
* @param mixed $senderName
* @return void
*/
public function sendApplicantAffectationNotification(User $user, Request $request, $senderName) : void
{
$notification = new ApplicantAffectation(
'Demande créée en votre nom',
$senderName . ' a créé la demande ' . $request->title . ' en votre nom.',
route('show', ['id' => $request->id]),
);
$this->sendMegaphoneNotification($user, $notification);
if($user->isMailEnabled()) {
$this->sendEmailNotification($user, $request, $notification, null, $senderName);
}
}
/**
* Sending a Megaphone application notification.
*
* @param User $user
* @param mixed $notification
* @return void
*/
public function sendMegaphoneNotification(User $user, $notification) : void
{
$user->notify($notification);
}
/**
* Sending an email notification.
*
* @param User $user
* @param Request $request
* @param mixed $notification
* @param Comment $comment
* @param string $senderName
* @return void
*/
public function sendEmailNotification(User $user, Request $request, $notification, $comment = null, $senderName = null) : void
{
$notifUuid = $user->notifications()->latest()->first()->id;
$toEmail = $user->isInfoManager() ? config('mail.info_notification_email') : $user->email;
if($notification instanceof NewRequest) {
Mail::to($toEmail)->queue(new NewRequestMail($request, $notifUuid));
} elseif ($notification instanceof NewComment) {
Mail::to($toEmail)->queue(new CommentMail($request, $comment, $notifUuid));
} elseif ($notification instanceof NewAffectation) {
Mail::to($toEmail)->queue(new AffectationMail($request, $notifUuid));
} elseif ($notification instanceof NewInvitation) {
Mail::to($toEmail)->queue(new InvitationMail($request, $senderName, $notifUuid));
} elseif ($notification instanceof FinishedRequest) {
Mail::to($toEmail)->queue(new FinishedRequestMail($request, $notifUuid));
} elseif ($notification instanceof ApplicantAffectation) {
Mail::to($toEmail)->queue(new ApplicantAffectationMail($request, $senderName, $notifUuid));
}
}
}
它允许我在控制器或 Livewire 组件上像这样使用它:
public function __construct()
{
$this->notificationService = new NotificationService();
}
[...]
$this->notificationService->sendFinishedRequestNotification($this->req->user, $this->req);
[...]
它在本地主机上运行良好(php artisanserve)。 但是当我将它部署在本地服务器(也称为生产)上时,它会抛出此错误:
The script tried to call a method on an incomplete object. Please ensure that the class definition "App\Mail\FinishedRequestMail" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide an autoloader to load the class definition
扩音器通知工作正常,但电子邮件通知不行。 所以看来在:
/**
* Sending notifications for finished request.
*
* @param User $user
* @param Request $request
* @return void
*/
public function sendFinishedRequestNotification(User $user, Request $request) : void
{
$notification = new FinishedRequest(
'Demande terminée',
'La demande : "' . $request->title . '" est terminée.',
route('show', ['id' => $request->id]),
);
$this->sendMegaphoneNotification($user, $notification);
if($user->isMailEnabled()) {
$this->sendEmailNotification($user, $request, $notification);
}
}
$this->sendMegaphoneNotification($user, $notification);
有效但无效$this->sendEmailNotification($user, $request, $notification);
在我的生产服务器上,我使用主管来处理电子邮件队列。
我无法在本地主机上重现该错误。
有人有想法吗?
我怀疑这与急切加载处理配置有关。 检查您的日志文件,了解有关急切加载“警告”(在本地)/“错误”(在生产中)的信息。 本地和生产之间的默认值有所不同——选项范围从悄悄地允许隐含的急切加载到抛出警告和错误。 我见过用户在 App\Providers\AppServiceProvider 中配置急切加载处理