[ Lumen 5.2 文档 ] 更多特性 -- 事件
1、简介
Lumen事件提供了简单的观察者模式实现,允许你订阅和监听应用中的事件。事件类通常存放在 app/Events目录,监听器存放在 app/Listeners。
2、和Laravel的区别
通常,Lumen中的事件函数和Laravel几乎一样,所以关于其使用可参考Laravel事件文档。Lumen也支持事件广播,从而允许你在客户端JavaScript中监听服务器端的事件。需要指出的是Lumen和Laravel的事件还是有一点区别。
生成器
在Lumen中,没有提供生成事件和监听器类的Artisan命令,所以你需要拷贝 ExampleEvent或 ExampleListener来自定义事件类和监听器类。
注册事件/监听器
和Laravel框架一样,Lumen内置 EventServiceProvider用于注册所有事件监听器。 listen属性是一个包含了所有事件(keys)及其对应监听器(values)的数组,你可以将应用的所有事件都添加到这个数组:
/** * The event listener mappings for the application. * * @var array */protected $listen = [ 'App\Events\ExampleEvent' => [ 'App\Listeners\ExampleListener', ],];
触发事件
你可以在应用中使用辅助函数 event或Event门面来触发事件,这些方法调用和Laravel一样:
event(new ExampleEvent);Event::fire(new ExampleEvent);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Alipay PHP...

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,
