admin 发表于 2023-3-12 15:01:28

在 ModStart 中使用 Event 事件监听来进行业务解耦

<p style="box-sizing:inherit;margin-top:0px;margin-bottom:20px;line-height:inherit;color:rgb(51,51,51);font-family:'-apple-system', BlinkMacSystemFont, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:16px;white-space:normal;background-color:rgb(255,255,255);">Laravel 中的事件通常用于业务解耦,比如注册成功后发邮件,短信,赠送优惠券,可以拆分代码,一个事件可以对应多个监听器,如果注册成功后同时发邮件,短信的话,可以只执行一个事件,调用不同的监听器。</p>

<p style="box-sizing:inherit;margin-top:0px;margin-bottom:20px;line-height:inherit;color:rgb(51,51,51);font-family:'-apple-system', BlinkMacSystemFont, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:16px;white-space:normal;background-color:rgb(255,255,255);">在 ModStart 中,如何快速的使用 Event 时间监听?</p>

<p style="box-sizing:inherit;margin-top:0px;margin-bottom:20px;line-height:inherit;color:rgb(51,51,51);font-family:'-apple-system', BlinkMacSystemFont, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:16px;white-space:normal;background-color:rgb(255,255,255);">我们以用户注册事件为例,定义事件类 MemberUserRegisteredEvent</p>

<pre class="hljs php">class MemberUserRegisteredEvent
{    
    public $memberUserId;    
    public function __construct($memberUserId)
    {        
        $this->memberUserId = $memberUserId;
    }
}</pre>

<p style="box-sizing:inherit;margin-top:0px;margin-bottom:20px;line-height:inherit;color:rgb(51,51,51);font-family:'-apple-system', BlinkMacSystemFont, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:16px;white-space:normal;background-color:rgb(255,255,255);"><br></p>

<p style="box-sizing:inherit;margin-top:0px;margin-bottom:20px;line-height:inherit;color:rgb(51,51,51);font-family:'-apple-system', BlinkMacSystemFont, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:16px;white-space:normal;background-color:rgb(255,255,255);">在用户注册的地方触发用户注册事件(假设注册用户的 ID 为 5)</p>

<pre class="hljs php">EventUtil::fire(new MemberUserRegisteredEvent(5));</pre>

<p style="box-sizing:inherit;margin-top:0px;margin-bottom:20px;line-height:inherit;color:rgb(51,51,51);font-family:'-apple-system', BlinkMacSystemFont, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:16px;white-space:normal;background-color:rgb(255,255,255);"><br></p>

<p style="box-sizing:inherit;margin-top:0px;margin-bottom:20px;line-height:inherit;color:rgb(51,51,51);font-family:'-apple-system', BlinkMacSystemFont, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:16px;white-space:normal;background-color:rgb(255,255,255);">在 ModuleServiceProvider 中监听事件</p>

<pre class="hljs php">use Illuminate\Support\Facades\Event;
class ModuleServiceProvider extends ServiceProvider
{    public function boot(Dispatcher $events)
    {
        Event ::listen(
             MemberUserRegisteredEvent::class, 
             function (MemberUserRegisteredEvent $e) {                  
                 // 用户注册了
                 // $e->memberUserId
             }
        );
    }
}</pre>

<p style="box-sizing:inherit;margin-top:0px;margin-bottom:0px;line-height:inherit;color:rgb(51,51,51);font-family:'-apple-system', BlinkMacSystemFont, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:16px;white-space:normal;background-color:rgb(255,255,255);"><br></p>

<p style="box-sizing:inherit;margin-top:0px;margin-bottom:0px;line-height:inherit;color:rgb(51,51,51);font-family:'-apple-system', BlinkMacSystemFont, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:16px;white-space:normal;background-color:rgb(255,255,255);">这样就可以在事件处理中增加用户注册后的操作</p>

<p><br></p>
页: [1]
查看完整版本: 在 ModStart 中使用 Event 事件监听来进行业务解耦