<?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/ 能夠發現已經訪問成功app
手動註冊一個事件監聽函數
/** * 事件監聽 */ public function listen(){ Event::listen('UserLogin', function ($user){ }); }
能夠使用命令行生成事件監聽this
php think make:listener UserLogin