Lumen 框架中 BroadcastServiceProvider 无法使用 Broadcast::routes() 提示 match 方法不存在问题

在 Laravel 中可用,但照搬一套代码用于 Lumen 时因为两边路由机制不一致会导致 match 方法不存在

在文件 vendor/illuminate/broadcasting/BroadcastManager.php 内的 routes 方法

/**
 * Register the routes for handling broadcast authentication and sockets.
 *
 * @param  array|null  $attributes
 * @return void
 */
public function routes(array $attributes = null)
{
     
    if ($this->app->routesAreCached()) {
     
        return;
    }

    $attributes = $attributes ?: ['middleware' => ['web']];

    $this->app['router']->group($attributes, function ($router) {
     
        // 此处会由于 Lumen 中 $router 实例为 Laravel\Lumen\Routing\Router 而非 Illuminate\Routing\Router
        // 导致 match 方法不存在
        $router->match(
            ['get', 'post'], '/broadcasting/auth',
            '\\'.BroadcastController::class.'@authenticate'
        );
    });
}

当然解决办法可不是去修改该文件,应当回到 BroadcastServiceProvider 修改 boot 方法

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot() {
     
    $this->app['router']->group([
        'middleware' => ['web']
    ], function (Router $router) {
     
        /** @noinspection PhpIncludeInspection */
        require_once base_path('routes/channels.php');
    });
}

最后在 routes/channels.php 中添加路由即可

/** @var Laravel\Lumen\Routing\Router $router */
$router->addRoute(
    ['get', 'post'], '/broadcasting/auth',
    '\\' . BroadcastController::class . '@authenticate'
);

你可能感兴趣的:(Web,Lumen,Broadcast,match)