Xử lý sự kiện được thực hiện như thế nào trong PHP?

tập lệnh java, trước tiên chúng tôi bao gồm thư viện jquery trực tuyến, bạn cũng có thể tải xuống thư viện và bao gồm nó từ máy chủ web của riêng bạn



lưu ý rằng tôi sử dụng jquery (và rất nhiều người chơi lớn trên web sử dụng jquery) và nếu bạn biết một chút java-script thì cú pháp có thể gây phiền nhiễu

Trong jquery, chúng tôi có một tập hợp lớn các phương thức mà chúng tôi có thể sử dụng trực tiếp như.

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
09 nếu bạn muốn sử dụng dữ liệu được trả về từ chức năng đó, chúng tôi tạo một chức năng gọi lại như

$.post( function(param_ returned_by_parent_function){
    //do stuf
});

Một cách khác để sử dụng jquery và đây thực sự là ý tưởng đằng sau nó là truy vấn một phần tử html và sau đó thực hiện các thao tác với nó như thế này

Các sự kiện của Laravel cung cấp một triển khai mẫu quan sát viên đơn giản, cho phép bạn đăng ký và lắng nghe các sự kiện khác nhau xảy ra trong ứng dụng của bạn. Các lớp sự kiện thường được lưu trữ trong thư mục

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

6, trong khi các đối tượng nghe của chúng được lưu trữ trong

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

7. Đừng lo lắng nếu bạn không thấy các thư mục này trong ứng dụng của mình vì chúng sẽ được tạo cho bạn khi bạn tạo các sự kiện và trình nghe bằng cách sử dụng các lệnh của bảng điều khiển Artisan

Các sự kiện đóng vai trò là một cách tuyệt vời để tách rời các khía cạnh khác nhau trong ứng dụng của bạn, vì một sự kiện đơn lẻ có thể có nhiều người nghe không phụ thuộc vào nhau. Ví dụ: bạn có thể muốn gửi thông báo Slack cho người dùng của mình mỗi khi đơn hàng được giao. Thay vì ghép mã xử lý đơn đặt hàng của bạn với mã thông báo Slack, bạn có thể tạo sự kiện

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

8 mà người nghe có thể nhận và sử dụng để gửi thông báo Slack

Đăng ký sự kiện và người nghe

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

9 đi kèm với ứng dụng Laravel của bạn cung cấp một nơi thuận tiện để đăng ký tất cả các trình lắng nghe sự kiện của ứng dụng của bạn. Thuộc tính

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

0 chứa một mảng tất cả các sự kiện (khóa) và đối tượng nghe (giá trị) của chúng. Bạn có thể thêm bao nhiêu sự kiện vào mảng này nếu ứng dụng của bạn yêu cầu. Ví dụ: hãy thêm một sự kiện

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

1

________số 8

Lưu ý
Lệnh

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

2 có thể được sử dụng để hiển thị danh sách tất cả các sự kiện và trình nghe được đăng ký bởi ứng dụng của bạn.

Tạo sự kiện và người nghe

Tất nhiên, việc tạo các tệp theo cách thủ công cho từng sự kiện và trình nghe là cồng kềnh. Thay vào đó, hãy thêm người nghe và sự kiện vào

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

3 của bạn và sử dụng lệnh Artisan

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

4. Lệnh này sẽ tạo bất kỳ sự kiện hoặc trình nghe nào được liệt kê trong

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

3 của bạn chưa tồn tại

Ngoài ra, bạn có thể sử dụng các lệnh Artisan

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

6 và

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

7 để tạo các sự kiện và trình nghe riêng lẻ

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
5

Đăng ký sự kiện theo cách thủ công

Thông thường, các sự kiện nên được đăng ký thông qua mảng

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

3

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

9;

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

Người nghe sự kiện ẩn danh có thể xếp hàng đợi

Khi đăng ký trình xử lý sự kiện dựa trên đóng theo cách thủ công, bạn có thể bọc trình nghe đóng trong hàm

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
82 để hướng dẫn Laravel thực thi trình nghe bằng cách sử dụng hàng đợi

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

Giống như queued jobs, bạn có thể sử dụng các phương thức

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
83,
$.post( function(param_ returned_by_parent_function){
    //do stuf
});
84 và
$.post( function(param_ returned_by_parent_function){
    //do stuf
});
85 để tùy chỉnh việc thực thi queued listener

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
8

Nếu bạn muốn xử lý các lỗi trình nghe xếp hàng đợi ẩn danh, bạn có thể cung cấp một bao đóng cho phương thức

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
86 trong khi xác định trình nghe
$.post( function(param_ returned_by_parent_function){
    //do stuf
});
87. Việc đóng này sẽ nhận được phiên bản sự kiện và phiên bản
$.post( function(param_ returned_by_parent_function){
    //do stuf
});
88 đã gây ra lỗi cho người nghe

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

0

Người nghe sự kiện ký tự đại diện

Bạn thậm chí có thể đăng ký người nghe bằng cách sử dụng

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
89 làm tham số ký tự đại diện, cho phép bạn nắm bắt nhiều sự kiện trên cùng một người nghe. Người nghe ký tự đại diện nhận tên sự kiện làm đối số đầu tiên và toàn bộ mảng dữ liệu sự kiện làm đối số thứ hai

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

2

khám phá sự kiện

Thay vì đăng ký sự kiện và người nghe theo cách thủ công trong mảng

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

9 của

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

3, bạn có thể kích hoạt tính năng khám phá sự kiện tự động. Khi bật tính năng khám phá sự kiện, Laravel sẽ tự động tìm và đăng ký các sự kiện và trình nghe của bạn bằng cách quét thư mục

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

02 của ứng dụng của bạn. Ngoài ra, mọi sự kiện được xác định rõ ràng được liệt kê trong

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

3 sẽ vẫn được đăng ký

Laravel tìm các trình lắng nghe sự kiện bằng cách quét các lớp trình nghe bằng các dịch vụ phản chiếu của PHP. Khi Laravel tìm thấy bất kỳ phương thức lớp trình nghe nào bắt đầu bằng

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

04 hoặc

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

05, Laravel sẽ đăng ký các phương thức đó dưới dạng trình lắng nghe sự kiện cho sự kiện được gợi ý kiểu trong chữ ký của phương thức

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

9

Tính năng phát hiện sự kiện bị tắt theo mặc định nhưng bạn có thể bật tính năng này bằng cách ghi đè phương thức

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

06 của ứng dụng

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

3 của bạn

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

2

Theo mặc định, tất cả các trình nghe trong thư mục

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

7 của ứng dụng của bạn sẽ được quét. Nếu bạn muốn xác định các thư mục bổ sung để quét, bạn có thể ghi đè phương thức

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

09 trong

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

3 của mình

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

6

Khám phá sự kiện trong sản xuất

Trong quá trình sản xuất, sẽ không hiệu quả nếu khung quét tất cả người nghe của bạn theo mọi yêu cầu. Do đó, trong quá trình triển khai của bạn, bạn nên chạy lệnh

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

21 Artisan để lưu vào bộ nhớ cache một bảng kê khai tất cả các sự kiện và trình lắng nghe của ứng dụng của bạn. Bảng kê khai này sẽ được khung sử dụng để tăng tốc quá trình đăng ký sự kiện. Lệnh

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

22 có thể được sử dụng để hủy bộ đệm

Xác định sự kiện

Một lớp sự kiện về cơ bản là một thùng chứa dữ liệu chứa thông tin liên quan đến sự kiện. Ví dụ: giả sử một sự kiện

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

8 nhận được một đối tượng ORM Eloquent

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
50

Như bạn có thể thấy, lớp sự kiện này không chứa logic. Đó là một vùng chứa cho phiên bản

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

24 đã được mua. Đặc điểm

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

25 được sử dụng bởi sự kiện sẽ tuần tự hóa bất kỳ mô hình Eloquent nào nếu đối tượng sự kiện được tuần tự hóa bằng cách sử dụng chức năng

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

26 của PHP, chẳng hạn như khi sử dụng trình nghe xếp hàng đợi

Xác định người nghe

Tiếp theo, hãy xem người nghe cho sự kiện ví dụ của chúng tôi. Người nghe sự kiện nhận các thể hiện sự kiện theo phương thức

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

04 của họ. Các lệnh Artisan

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

4 và

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

7 sẽ tự động nhập lớp sự kiện thích hợp và gõ gợi ý sự kiện trên phương thức

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

04. Trong phương thức

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

04, bạn có thể thực hiện bất kỳ hành động nào cần thiết để phản hồi sự kiện

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
51

Lưu ý
Người nghe sự kiện của bạn cũng có thể nhập gợi ý bất kỳ phụ thuộc nào họ cần vào hàm tạo của họ. Tất cả các trình lắng nghe sự kiện đều được giải quyết thông qua bộ chứa dịch vụ của Laravel, do đó, các phần phụ thuộc sẽ được đưa vào tự động.

Dừng việc tuyên truyền một sự kiện

Đôi khi, bạn có thể muốn ngừng lan truyền một sự kiện tới những người nghe khác. Bạn có thể làm như vậy bằng cách trả lại

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

92 từ phương thức

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

04 của người nghe của bạn

Người nghe sự kiện xếp hàng đợi

Trình nghe xếp hàng có thể có lợi nếu trình nghe của bạn sẽ thực hiện một tác vụ chậm như gửi email hoặc thực hiện yêu cầu HTTP. Trước khi sử dụng trình xử lý xếp hàng, hãy đảm bảo định cấu hình hàng đợi của bạn và khởi động trình xử lý hàng đợi trên máy chủ hoặc môi trường phát triển cục bộ của bạn

Để chỉ định rằng một người nghe nên được xếp hàng đợi, hãy thêm giao diện

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

94 vào lớp người nghe. Trình nghe được tạo bởi các lệnh Artisan

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

4 và

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

7 đã có giao diện này được nhập vào không gian tên hiện tại để bạn có thể sử dụng nó ngay lập tức

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
52

Đó là nó. Bây giờ, khi một sự kiện được xử lý bởi bộ lắng nghe này được gửi đi, bộ lắng nghe sẽ tự động được xếp hàng bởi bộ điều phối sự kiện bằng cách sử dụng hệ thống hàng đợi của Laravel. Nếu không có ngoại lệ nào được đưa ra khi trình nghe được thực thi bởi hàng đợi, công việc được xếp hàng đợi sẽ tự động bị xóa sau khi xử lý xong

Tùy chỉnh kết nối hàng đợi & Tên hàng đợi

Nếu bạn muốn tùy chỉnh kết nối hàng đợi, tên hàng đợi hoặc thời gian trì hoãn hàng đợi của trình xử lý sự kiện, bạn có thể xác định các thuộc tính

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

97,

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

98 hoặc

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

99 trên lớp trình xử lý của mình

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
53

Nếu bạn muốn xác định kết nối hàng đợi của người nghe hoặc tên hàng đợi trong thời gian chạy, bạn có thể xác định các phương thức

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

20 hoặc

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

21 trên người nghe

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
54

Người nghe xếp hàng có điều kiện

Đôi khi, bạn có thể cần xác định xem một người nghe có nên được xếp hàng đợi hay không dựa trên một số dữ liệu chỉ khả dụng trong thời gian chạy. Để thực hiện điều này, một phương pháp

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

22 có thể được thêm vào một người nghe để xác định xem người nghe có nên được xếp hàng đợi hay không. Nếu phương thức

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

22 trả về

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

92, người nghe sẽ không được thực thi

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
55

Tương tác thủ công với hàng đợi

Nếu bạn cần truy cập thủ công các phương thức

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

25 và

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

26 của công việc xếp hàng cơ bản của người nghe, bạn có thể làm như vậy bằng cách sử dụng đặc điểm

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

27. Đặc điểm này được nhập theo mặc định trên các trình nghe được tạo và cung cấp quyền truy cập vào các phương thức này

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
56

Trình xử lý sự kiện được xếp hàng & Giao dịch cơ sở dữ liệu

Khi các trình nghe trong hàng đợi được gửi đi trong các giao dịch cơ sở dữ liệu, chúng có thể được hàng đợi xử lý trước khi giao dịch cơ sở dữ liệu được thực hiện. Khi điều này xảy ra, mọi cập nhật bạn đã thực hiện đối với mô hình hoặc bản ghi cơ sở dữ liệu trong quá trình giao dịch cơ sở dữ liệu có thể chưa được phản ánh trong cơ sở dữ liệu. Ngoài ra, bất kỳ mô hình hoặc bản ghi cơ sở dữ liệu nào được tạo trong giao dịch có thể không tồn tại trong cơ sở dữ liệu. Nếu trình nghe của bạn phụ thuộc vào các mô hình này, lỗi không mong muốn có thể xảy ra khi xử lý công việc gửi trình nghe đã xếp hàng đợi

Nếu tùy chọn cấu hình

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

28 của kết nối hàng đợi của bạn được đặt thành

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

92, thì bạn vẫn có thể chỉ ra rằng một trình nghe xếp hàng cụ thể sẽ được gửi đi sau khi tất cả các giao dịch cơ sở dữ liệu mở đã được thực hiện bằng cách xác định thuộc tính

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

60 trên lớp trình nghe

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
57

Lưu ý
Để tìm hiểu thêm về cách khắc phục những sự cố này, vui lòng xem lại tài liệu liên quan đến công việc được xếp hàng đợi và giao dịch cơ sở dữ liệu.

Xử lý công việc thất bại

Đôi khi người nghe sự kiện xếp hàng đợi của bạn có thể thất bại. Nếu queued listener vượt quá số lần thử tối đa do queue worker của bạn xác định, thì phương thức

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

61 sẽ được gọi trên listener của bạn. Phương thức

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

61 nhận thể hiện sự kiện và
$.post( function(param_ returned_by_parent_function){
    //do stuf
});
88 gây ra lỗi

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
58

Chỉ định số lần thử tối đa của người nghe xếp hàng

Nếu một trong những người nghe trong hàng đợi của bạn gặp lỗi, bạn có thể không muốn nó tiếp tục thử lại vô thời hạn. Do đó, Laravel cung cấp nhiều cách khác nhau để chỉ định số lần hoặc khoảng thời gian một người nghe có thể được thử

Bạn có thể xác định thuộc tính

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

64 trên lớp trình nghe của mình để chỉ định số lần trình nghe có thể được thử trước khi nó được coi là không thành công

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
59

Là một giải pháp thay thế cho việc xác định số lần người nghe có thể được thử trước khi nó không thành công, bạn có thể xác định thời điểm mà người nghe không còn được thử nữa. Điều này cho phép người nghe được thử bất kỳ số lần nào trong một khung thời gian nhất định. Để xác định thời gian mà tại đó người nghe không còn được thử nữa, hãy thêm phương thức

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

65 vào lớp người nghe của bạn. Phương thức này sẽ trả về một phiên bản

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

66

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

0

gửi sự kiện

Để gửi một sự kiện, bạn có thể gọi phương thức tĩnh

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

67 trên sự kiện. Phương pháp này được cung cấp trên sự kiện bởi đặc điểm

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

68. Mọi đối số được truyền cho phương thức

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

67 sẽ được truyền cho hàm tạo của sự kiện

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

1

Nếu bạn muốn gửi một sự kiện có điều kiện, bạn có thể sử dụng các phương thức

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
500 và
$.post( function(param_ returned_by_parent_function){
    //do stuf
});
501

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

2

Lưu ý
Khi thử nghiệm, có thể hữu ích khi khẳng định rằng một số sự kiện nhất định đã được gửi đi mà không thực sự kích hoạt trình nghe của chúng. Trình trợ giúp thử nghiệm tích hợp sẵn của Laravel làm cho nó trở nên dễ dàng.

Người đăng ký sự kiện

Viết người đăng ký sự kiện

Người đăng ký sự kiện là các lớp có thể đăng ký nhiều sự kiện từ chính lớp người đăng ký, cho phép bạn xác định một số trình xử lý sự kiện trong một lớp. Người đăng ký phải xác định phương thức

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
502, phương thức này sẽ được chuyển qua một phiên bản điều phối sự kiện. Bạn có thể gọi phương thức

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

0 trên bộ điều phối đã cho để đăng ký người nghe sự kiện

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

3

Nếu các phương thức lắng nghe sự kiện của bạn được xác định trong chính người đăng ký, bạn có thể thấy thuận tiện hơn khi trả về một mảng các sự kiện và tên phương thức từ phương thức

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
502 của người đăng ký. Laravel sẽ tự động xác định tên lớp của người đăng ký khi đăng ký người nghe sự kiện

use App\Events\PodcastProcessed;

use App\Listeners\SendPodcastNotification;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

[SendPodcastNotification::class, 'handle']

Event::listen(function (PodcastProcessed $event) {

4

Đăng ký người đăng ký sự kiện

Sau khi viết người đăng ký, bạn đã sẵn sàng để đăng ký nó với người điều phối sự kiện. Bạn có thể đăng ký người đăng ký bằng thuộc tính

$.post( function(param_ returned_by_parent_function){
    //do stuf
});
505 trên

use App\Events\PodcastProcessed;

use function Illuminate\Events\queueable;

use Illuminate\Support\Facades\Event;

* Register any other events for your application.

Event::listen(queueable(function (PodcastProcessed $event) {

3. Ví dụ: hãy thêm
$.post( function(param_ returned_by_parent_function){
    //do stuf
});
507 vào danh sách

Cách xử lý sự kiện được thực hiện trong PHP?

Cách thức hoạt động .
Người dùng mở một trang web
Để tạo mã HTML của trang, mã PHP của ứng dụng được thực thi
Mã PHP của ứng dụng chứa sự kiện. Nó đã được thêm bởi nhà phát triển ứng dụng
Một sự kiện được xử lý bởi một plugin. Điều này là do nhà phát triển plugin đã quyết định như vậy

Làm thế nào xử lý sự kiện được thực hiện giải thích?

Xử lý sự kiện là cơ chế kiểm soát sự kiện và quyết định điều gì sẽ xảy ra nếu một sự kiện xảy ra. Cơ chế này có mã được gọi là trình xử lý sự kiện được thực thi khi một sự kiện xảy ra. Java sử dụng Mô hình sự kiện ủy quyền để xử lý các sự kiện .

Làm cách nào để tạo một sự kiện trong PHP?

Sự kiện ¶ .
Biến cố. thêm - Làm cho sự kiện đang chờ xử lý
Biến cố. addSignal — Bí danh của sự kiện. cộng
Biến cố. addTimer — Bí danh của sự kiện. cộng
Biến cố. __construct — Xây dựng đối tượng Sự kiện
Biến cố. del - Làm cho sự kiện không chờ xử lý
Biến cố. delSignal — Bí danh của sự kiện. xóa
Biến cố. delTimer - Bí danh của sự kiện. xóa

Ba thành phần chính của xử lý sự kiện là gì?

Việc xử lý sự kiện có ba thành phần chính, .
Sự kiện. Một sự kiện là một sự thay đổi trạng thái của một đối tượng
Nguồn sự kiện. Nguồn sự kiện là đối tượng tạo ra sự kiện
người nghe. Một người nghe là một đối tượng lắng nghe sự kiện. Một người nghe được thông báo khi một sự kiện xảy ra