thinkphp中如何使用event事件?
ThinkPHP6.0 event(事件)的使用方法
新版的事件系统可以看成是5.1版本行为系统的升级版,事件系统相比行为系统强大的地方在于事件本身可以是一个类,并且可以更好的支持事件订阅者。
事件相比较中间件的优势是事件比中间件更加精准定位(或者说粒度更细),并且更适合一些业务场景的扩展。例如,我们通常会遇到用户注册或者登录后需要做一系列操作,通过事件系统可以做到不侵入原有代码完成登录的操作扩展,降低系统的耦合性的同时,也降低了BUG的可能性。
TP6.0 文档中关于事件写了很多,有定义事件,有事件监听,还有事件订阅,实在让人头大
在网上翻了多篇博文,最终发现,它们是事件的不同实现方式 /(ㄒoㄒ)/~~
1.事件绑定
事件绑定不如其他两种方法使用方便,这里就暂不介绍了
2.事件监听
2-1.手动注册事件监听
我们可以手动注册一个事件监听
<?php namespace app\controller; use think\facade\Event; class Index { public function __construct(){ Event::listen('first', function($param){ echo $param . '+'; }); } public function index() { event('first', 'first success'); return 111; } }
2-2.使用监听类实现监听
首先我们通过命令行快速生成一个监听类
php think make:listener TestListener
修改 TestListener.php 文件代码
<?php declare (strict_types = 1); namespace app\listener; class TestListener { /** * 事件监听处理 * * @return mixed */ public function handle($event) { // echo "testListner监听成功"; } }
然后我们在index控制器中注册监听事件
<?php namespace app\controller; use think\facade\Event; class Index { public function index() { Event::listen('test', 'app\listener\TestListener'); event('test'); return 111; } }
此时我们访问index控制器下的index方法就可以看到监听成功的返回了
当然我们有更简便的方法注册监听类!
修改event.php文件,添加事件监听
<?php // 事件定义文件 return [ 'bind' => [ ], 'listen' => [ 'AppInit' => [], 'HttpRun' => [], 'HttpEnd' => [], 'LogLevel' => [], 'LogWrite' => [], 'test' => ['app\listener\TestListener'] ], 'subscribe' => [ ], ];
3.事件订阅
可以通过事件订阅机制,在一个监听器中监听多个事件
首先我们通过命令行生成一个订阅类 TestSub.php
<?php declare (strict_types = 1); namespace app\subscribe; class TestSub { public function onTestSub1(){ echo("testSub1"); } public function onTestSub2(){ echo("testSub2"); } }
然后调用这两个事件
<?php namespace app\controller; use think\facade\Event; use think\facade\Db; class Index { public function index() { event('TestSub1'); event('TestSub2'); return 111; } }
此时访问该接口就可以看到这两个事件的结果了
4.一些小心得
事件可以被主方法捕获异常!!!
主方法开启事务后,事件中若出现数据库错误,主方法可以捕获该异常并进行回滚等操作
首先,我们在 TestSub.php,对两个事件都进行数据库操作,其中一个数据库操作会报错
<?php declare (strict_types = 1); namespace app\subscribe; class TestSub { public function onTestSub1(){ echo("testSub1"); $data = ['id' => '1', 'username' => 'haha1']; \think\facade\Db::name('test')->save($data); echo("testSub11"); } public function onTestSub2(){ echo("testSub2"); $data = ['id' => '1', 'username' => 'haha1']; //test1表并不存在 \think\facade\Db::name('test1')->save($data); echo("testSub22"); } }
然后调用这两个事件,并开启事务
<?php namespace app\controller; use think\facade\Event; use think\facade\Db; class Index { public function index() { Db::startTrans(); try { event('TestSub1'); event('TestSub2'); // 提交事务 Db::commit(); return 111; } catch (\Exception $e) { // 回滚事务 Db::rollback(); echo($e); return 222; } } }
更多相关知识,请访问PHP中文网!

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor