laravel-octane-workerman 現已支援引入 webman 外掛

蝸牛發表於2022-04-03

訪問地址 laravel-workerman.iwnweb.com/d/7-l...

github.com/mouyong/laravel-octane-...


1. 新增外掛安裝指令碼到專案中

在專案的 composer.json 新增如下內容。可參考 laravel-octane-workermancomposer.json 中,scripts 配置

{
    // ...
    "scripts": {
        "post-package-install": [
            "JieAnthony\\LaravelOctaneWorkerman\\WebmanPlugin::install"
        ],
        "post-package-update": [
            "JieAnthony\\LaravelOctaneWorkerman\\WebmanPlugin::install"
        ],
        "pre-package-uninstall": [
            "JieAnthony\\LaravelOctaneWorkerman\\WebmanPlugin::uninstall"
        ],
        // ...
    }
    // ...
}

2. 引入 webman 外掛

composer require webman/push -vvv

3. 適配 laravel 框架

1. 路由適配

  • 修改外掛的 route.php 檔案
  • Webman\Request 替換為 Illuminate\Http\Request
  • Webman\Route 替換為 Illuminate\Routing\Router
  • 使用 laravel 的路由分組包含路由
  • 替換 Route:: 呼叫為 $route->

示例:

// use support\Request;
// use Webman\Route;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;

app('router')->middleware(['web'])->group(function (Router $route) {
    /**
     * 推送js客戶端檔案
     */
    // here before
    // Route::any('plugin/webman/push/push.js', function (Request $request) {
    //    return response()->file(base_path() . '/vendor/webman/push/src/push.js');
    // });

    // here after
    $route->any('plugin/webman/push/push.js', function (Request $request) {
        return response()->file(base_path() . '/vendor/webman/push/src/push.js');
    });
});
  • 手動執行 composer dump-autoload,讓專案自動自行 @php artisan package:discover --ansi 命令
    composer du # 簡寫
本作品採用《CC 協議》,轉載必須註明作者和本文連結
Study hard and make progress every day. Study hard and make progress every day.

相關文章