<?php
namespace app\index\controller;
use app\index\model\User;
class Index
{
public function Index(){
// 觸發UserLogin事件 使用助手函數
event('UserLogin');
return "ming";
}
}
複製代碼
php think make:event UserLogin
複製代碼
<?php
namespace app\event;
use app\index\model\User;
class UserLogin{
public $user;
public function _construct(User $user){
$this->user = $user;
}
}
複製代碼
這裏依賴於model目錄下的User類php
此時目錄以下 thinkphp
添加標識apache
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 事件定義文件
return [
'bind' => [
'UserLogin' => 'app\event\UserLogin',
],
'listen' => [
'AppInit' => [],
'HttpRun' => [],
'HttpEnd' => [],
'LogLevel' => [],
'LogWrite' => [],
],
'subscribe' => [
],
];
複製代碼
此時訪問 http://localhost:8082/ 能夠發現已經訪問成功bash
手動註冊一個事件監聽app
/**
* 事件監聽
*/
public function listen(){
Event::listen('UserLogin', function ($user){
});
}
複製代碼
可使用命令行生成事件監聽函數
php think make:listener UserLogin
複製代碼