event.php
<?php
// 事件定义文件
return [
'bind' => [
],
'listen' => [
'AppInit' => [],
'HttpRun' => [],
'HttpEnd' => [],
'LogLevel' => [],
'LogWrite' => [],
'demolistener' => ['\crmeb\listeners\Demolistener'],
],
'subscribe' => [
crmeb\subscribes\Demosubscribe::class,
],
];
Demoevent.php
<?php
namespace crmeb\event;
class Demoevent
{
public function __construct()
{
echo '我是事件类!';
}
}
Demolistener.php
<?php
namespace crmeb\listeners;
class Demolistener
{
public function handle($params)
{
echo $params;
echo "事件监听处理";
}
}
Demosubscribe.php
<?php
namespace crmeb\subscribes;
class Demosubscribe
{
public function onDemoLogin($params)
{
echo $params;
echo 'DemoLogin事件响应处理';
// 事件响应处理
}
public function onDemoLogout($params)
{
echo $params;
echo 'DemoLogout事件响应处理';
// 事件响应处理
}
}
测试类PublicController.php
<?php
namespace app\api\controller\demo;
use think\facade\Config;
use app\Request;
use think\facade\Event;
/**
* 公共类
* Class PublicController
* @package app\api\controller
*/
class PublicController
{
//触发事件类
//http://meishu.local/api/demo/demoevent
public function demoevent(){
echo 11;
Event::trigger(new \crmeb\event\Demoevent());
event(new \crmeb\event\Demoevent());
}
//使用监听类来执行监听
//http://meishu.local/api/demo/demolistener
public function demolistener(){
echo 11;
Event::trigger('demolistener','哈哈哈');
event('demolistener','哎呦喂');
}
//触发事件订阅者中方法
//http://meishu.local/api/demo/demosubscribe
public function demosubscribe(){
echo 11;
Event::trigger('DemoLogin','哈哈哈');
event('DemoLogout','哎呦喂');
}
}
路由文件demo.php
<?php
use think\facade\Route;
/**
* demo 版本路由
*/
Route::group('demo', function () {
Route::get('demoevent', 'demo.PublicController/demoevent');
Route::get('demolistener', 'demo.PublicController/demolistener');
Route::get('demosubscribe', 'demo.PublicController/demosubscribe');
})->middleware(\app\http\middleware\AllowOriginApiMiddleware::class);