[laravel源码]Dispatcher/dispath&fire解析

说明#

本章说明事件分配 dispatch 的流程。

源码#

vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php

public function fire($event, $payload = [], $halt = false)
{
    return $this->dispatch($event, $payload, $halt);
}

public function dispatch($event, $payload = [], $halt = false)
{
    // 1. 数据整理
    list($event, $payload) = $this->parseEventAndPayload(
        $event, $payload
    );

    // 2. 判断载荷是否有广播事件
    if ($this->shouldBroadcast($payload)) {
        $this->broadcastEvent($payload[0]);
    }
    $responses = [];

    // 3. 获取监听器并调用
    foreach ($this->getListeners($event) as $listener) {
        $response = $listener($event, $payload);
        if ($halt && ! is_null($response)) {
            return $response;
        }
        if ($response === false) {
            break;
        }
        $responses[] = $response;
    }

    return $halt ? null : $responses;
}

分析#

本作品采用《CC 协议》,转载必须注明作者和本文链接
城里的野山参
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
文章
68
粉丝
10
喜欢
29
收藏
41
排名:563
访问:1.4 万
私信
所有博文
社区赞助商