为什么我的事件侦听器没有被触发?

Why my event listener isnt being triggered?

提问人:Kubura 提问时间:8/27/2023 最后编辑:matiaslauritiKubura 更新时间:8/27/2023 访问量:59

问:

我想在创建收据后向用户发送电子邮件。我有一个收据控制器,它创建一个控制器,然后触发一个事件,收据创建事件,它应该触发一个侦听器,发送收据通知,它应该发送邮件(我先用一个工作尝试过)。 法典: 收据创建事件:

<?php
 
namespace App\Events;
 
use App\Models\Racun;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
 
class ReceiptCreatedEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
 
    /**
     * Create a new event instance.
     */
 
    public function __construct(public Racun $receipt)
    {
 
        $this->receipt = $receipt;
    }
 
    /**
     * Get the channels the event should broadcast on.
     *
     * @return array<int, \Illuminate\Broadcasting\Channel>
     */
    public function broadcastOn(): array
    {
        return [
            new PrivateChannel('channel-name'),
        ];
    }
}

听者:

<?php
 
namespace App\Listeners;
 
use App\Events\ReceiptCreatedEvent;
use App\Mail\ReceiptCreatedNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Mail;
 
class SendReceiptNotification
{
    /**
     * Create the event listener.
     */
    public function __construct()
    {
        //
    }
 
    /**
     * Handle the event.
     */
    public function handle(ReceiptCreatedEvent $event): void
    {   info("This is the receipt: " . $event->receipt);
        Mail::to($event->receipt->user->email)->send(new ReceiptCreatedNotification($event->receipt, $event->receipt->user));
        //SendMailJob::dispatch($event->receipt);
    }
}

收据控制器:

<?php
 
namespace App\Http\Controllers;
 
use App\Events\ReceiptCreatedEvent;
use App\Mail\ReceiptCreatedNotification;
use App\Models\Artikal;
use App\Models\Racun;
use Illuminate\Foundation\Auth\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Response;
 
class RacunController extends Controller
{
    //
    public function ajaxStore(Request $request){
        $this->authorize('storeReceipt', auth()->user());
        $data = $request->all();
 
        $racun = Racun::create(
            [
                'user_id' => $data['user_id'],
                'hladnjaca_id' => $data['hladnjaca_id'],
                'artikal_id' => $data['artikal'],
                'datum' => now()->format('Y-m-d H:i:s'),
                'ulaz_gajbi' => $data['ulaz'],
                'izlaz_gajbi' => $data['izlaz'],
                'bruto' => $data['bruto'],
                'neto' => $data['neto'],
                'cena_po_kg_pdv' => Artikal::find($data['artikal'])->cena_sa_pdvom,
                'cena_po_kg_bez_pdv' => Artikal::find($data['artikal'])->cena_bez_pdva,
                'ukupno_cena_pdv' => $data['iznos_sa_pdvom'],
                'ukupno_cena_bez_pdv'=> $data['iznos_bez_pdva']
            ]
        );
       //Mail::to($racun->user->email)->send(new ReceiptCreatedNotification($racun, $racun->user));
 
        $response = [
            'message' => 'success'
        ];
       ReceiptCreatedEvent::dispatch($racun);
       return Response::json($response);
    }
}

活动和听众的注册:

protected $listen = [
    Registered::class => [
 
       SendEmailVerificationNotification::class,
    ],
    ReceiptCreatedEvent::class => [
        SendReceiptNotification::class,
    ],
];

邮件有效,我已经在没有侦听器的情况下尝试过,所以这不是问题。侦听器没有触发,所以日志中没有任何内容,我尝试过 info() 助手,但什么都没有......

PHP Laravel 后端 事件侦听

评论

0赞 UnderDog 8/27/2023
如果执行 dd($receipt),则查看是否收到。然后在构造函数的侦听器中执行完全相同的操作,但随后要执行$event
1赞 Kubura 8/27/2023
在构造函数事件中,我做了 info() 并且它起作用了,它正确地将$receipt记录在日志中。但是侦听器不起作用,我已将 info() 放在构造函数和 handle 方法中,只是没有调用侦听器。我真的不知道可能是什么问题......
1赞 Kubura 8/28/2023
我已经解决了。我不知道问题出在哪里,但在运行一堆工匠命令后肯定开始工作,我相信 php 工匠清晰编译完成了这项工作,但不确定。Composer -dump autoload 不是那个(做了 10 次)。无论如何,感谢您的帮助

答: 暂无答案