事件
绑定事件
- 通过模块根目录
events.json
文件绑定。如[
{
"group_title": "分组名称",
"list": [
{
"event": "TestEvent",
"description": "事件描述",
"payload": "载荷描述",
"listeners": [
{
"name": "app\\test\\events\\Event@onTest",
"description": "带方法,执行Event类onTest方法",
"sort": 10,
"status": 1
},
{
"name": "app\\test\\events\\Event",
"description": "不带方法,执行Event类handle方法",
"sort": 10,
"status": 1
},
]
}
]
}
]
- 通过根目录
event.php
订阅或监听return [
'listen' => [
'Test' => [
'\\app\\test\\events\\Event@test1',
'\\app\\test\\events\\Event@test2'
]
],
'subscribe' => [
'\\app\\test\\subscribe\\Event'
]
];
// \app\test\subscribe
class Event
{
public function onTestEvent()
{
echo 'event trigger';
}
}
- 动态绑定,如
app(\yi\Event::class)->bind('BeforeIndexController', function($payload) {
// do something
});
app(\yi\Event::class)->bind('BeforeIndexController', '\\app\\test\\events\\Event@test');
触发事件
event('事件名称', 参数)
ev('事件名称', 参数1, 参数2 ...)
该方式带返回值,如:public function onTest($payload)
{
list($p1, $p2) = $payload->params; // 获取参数
$payload->result = "param1: $p1, param2: $p2";
}
// 触发事件
$data = ev('test', 'p1', 'p2');
echo $data; // param1: p1, param2: p2
<h3 id="h3-u7ED1u5B9Au4E8Bu4EF6"><a name="绑定事件" class="reference-link"></a><span class="header-link octicon octicon-link"></span>绑定事件</h3><ol>
<li>通过模块根目录 <code>events.json</code> 文件绑定。如<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pun">[</span></code></li><li class="L1"><code><span class="pln"> </span><span class="pun">{</span></code></li><li class="L2"><code><span class="pln"> </span><span class="str">"group_title"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"分组名称"</span><span class="pun">,</span></code></li><li class="L3"><code><span class="pln"> </span><span class="str">"list"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span></code></li><li class="L4"><code><span class="pln"> </span><span class="pun">{</span></code></li><li class="L5"><code><span class="pln"> </span><span class="str">"event"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"TestEvent"</span><span class="pun">,</span></code></li><li class="L6"><code><span class="pln"> </span><span class="str">"description"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"事件描述"</span><span class="pun">,</span></code></li><li class="L7"><code><span class="pln"> </span><span class="str">"payload"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"载荷描述"</span><span class="pun">,</span></code></li><li class="L8"><code><span class="pln"> </span><span class="str">"listeners"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span></code></li><li class="L9"><code><span class="pln"> </span><span class="pun">{</span></code></li><li class="L0"><code><span class="pln"> </span><span class="str">"name"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"app\\test\\events\\Event@onTest"</span><span class="pun">,</span></code></li><li class="L1"><code><span class="pln"> </span><span class="str">"description"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"带方法,执行Event类onTest方法"</span><span class="pun">,</span></code></li><li class="L2"><code><span class="pln"> </span><span class="str">"sort"</span><span class="pun">:</span><span class="pln"> </span><span class="lit">10</span><span class="pun">,</span></code></li><li class="L3"><code><span class="pln"> </span><span class="str">"status"</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1</span></code></li><li class="L4"><code><span class="pln"> </span><span class="pun">},</span></code></li><li class="L5"><code><span class="pln"> </span><span class="pun">{</span></code></li><li class="L6"><code><span class="pln"> </span><span class="str">"name"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"app\\test\\events\\Event"</span><span class="pun">,</span></code></li><li class="L7"><code><span class="pln"> </span><span class="str">"description"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"不带方法,执行Event类handle方法"</span><span class="pun">,</span></code></li><li class="L8"><code><span class="pln"> </span><span class="str">"sort"</span><span class="pun">:</span><span class="pln"> </span><span class="lit">10</span><span class="pun">,</span></code></li><li class="L9"><code><span class="pln"> </span><span class="str">"status"</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1</span></code></li><li class="L0"><code><span class="pln"> </span><span class="pun">},</span></code></li><li class="L1"><code><span class="pln"> </span><span class="pun">]</span></code></li><li class="L2"><code><span class="pln"> </span><span class="pun">}</span></code></li><li class="L3"><code><span class="pln"> </span><span class="pun">]</span></code></li><li class="L4"><code><span class="pln"> </span><span class="pun">}</span></code></li><li class="L5"><code><span class="pun">]</span></code></li></ol></pre></li><li>通过根目录 <code>event.php</code> 订阅或监听<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="kwd">return</span><span class="pln"> </span><span class="pun">[</span></code></li><li class="L1"><code><span class="pln"> </span><span class="str">'listen'</span><span class="pln"> </span><span class="pun">=></span><span class="pln"> </span><span class="pun">[</span></code></li><li class="L2"><code><span class="pln"> </span><span class="str">'Test'</span><span class="pln"> </span><span class="pun">=></span><span class="pln"> </span><span class="pun">[</span></code></li><li class="L3"><code><span class="pln"> </span><span class="str">'\\app\\test\\events\\Event@test1'</span><span class="pun">,</span></code></li><li class="L4"><code><span class="pln"> </span><span class="str">'\\app\\test\\events\\Event@test2'</span></code></li><li class="L5"><code><span class="pln"> </span><span class="pun">]</span></code></li><li class="L6"><code><span class="pln"> </span><span class="pun">],</span></code></li><li class="L7"><code><span class="pln"> </span><span class="str">'subscribe'</span><span class="pln"> </span><span class="pun">=></span><span class="pln"> </span><span class="pun">[</span></code></li><li class="L8"><code><span class="pln"> </span><span class="str">'\\app\\test\\subscribe\\Event'</span></code></li><li class="L9"><code><span class="pln"> </span><span class="pun">]</span></code></li><li class="L0"><code><span class="pun">];</span></code></li><li class="L1"><code><span class="com">// \app\test\subscribe</span></code></li><li class="L2"><code><span class="kwd">class</span><span class="pln"> </span><span class="typ">Event</span><span class="pln"> </span></code></li><li class="L3"><code><span class="pun">{</span></code></li><li class="L4"><code><span class="pln"> </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">function</span><span class="pln"> onTestEvent</span><span class="pun">()</span></code></li><li class="L5"><code><span class="pln"> </span><span class="pun">{</span></code></li><li class="L6"><code><span class="pln"> echo </span><span class="str">'event trigger'</span><span class="pun">;</span></code></li><li class="L7"><code><span class="pln"> </span><span class="pun">}</span></code></li><li class="L8"><code><span class="pun">}</span></code></li></ol></pre></li><li>动态绑定,如<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pln">app</span><span class="pun">(</span><span class="pln">\yi\Event</span><span class="pun">::</span><span class="kwd">class</span><span class="pun">)-></span><span class="pln">bind</span><span class="pun">(</span><span class="str">'BeforeIndexController'</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">$payload</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L1"><code><span class="pln"> </span><span class="com">// do something</span></code></li><li class="L2"><code><span class="pun">});</span></code></li><li class="L3"><code><span class="pln">app</span><span class="pun">(</span><span class="pln">\yi\Event</span><span class="pun">::</span><span class="kwd">class</span><span class="pun">)-></span><span class="pln">bind</span><span class="pun">(</span><span class="str">'BeforeIndexController'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'\\app\\test\\events\\Event@test'</span><span class="pun">);</span></code></li></ol></pre></li></ol>
<h3 id="h3-u89E6u53D1u4E8Bu4EF6"><a name="触发事件" class="reference-link"></a><span class="header-link octicon octicon-link"></span>触发事件</h3><ol>
<li><p><code>event('事件名称', 参数)</code> </p>
</li><li><p><code>ev('事件名称', 参数1, 参数2 ...)</code><br>该方式带返回值,如:
</p><pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="kwd">public</span><span class="pln"> </span><span class="kwd">function</span><span class="pln"> onTest</span><span class="pun">(</span><span class="pln">$payload</span><span class="pun">)</span></code></li><li class="L1"><code><span class="pun">{</span></code></li><li class="L2"><code><span class="pln"> list</span><span class="pun">(</span><span class="pln">$p1</span><span class="pun">,</span><span class="pln"> $p2</span><span class="pun">)</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> $payload</span><span class="pun">-></span><span class="kwd">params</span><span class="pun">;</span><span class="pln"> </span><span class="com">// 获取参数</span></code></li><li class="L3"><code><span class="pln"> $payload</span><span class="pun">-></span><span class="pln">result </span><span class="pun">=</span><span class="pln"> </span><span class="str">"param1: $p1, param2: $p2"</span><span class="pun">;</span></code></li><li class="L4"><code><span class="pun">}</span></code></li><li class="L5"><code><span class="com">// 触发事件</span></code></li><li class="L6"><code><span class="pln"> $data </span><span class="pun">=</span><span class="pln"> ev</span><span class="pun">(</span><span class="str">'test'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'p1'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'p2'</span><span class="pun">);</span></code></li><li class="L7"><code><span class="pln"> echo $data</span><span class="pun">;</span><span class="pln"> </span><span class="com">// param1: p1, param2: p2</span></code></li></ol></pre></li></ol>