首页 > 解决方案 > Laravel - Livewire,如何自定义全局消息 URL?

问题描述

在此示例中,使用如下示例中的自定义域不会告诉 Livewire 请求以相同的子域作为前缀subdomain1

    Route::domain('subdomain1.'.env('APP_DOMAIN', 'localhost'))
             ->middleware(['web',.....])
             ->as('app.')
             ->group(base_path('routes/app.php'));

问题是 Livewire 获取端点的默认回调/livewire/message/{message}不幸的是仅使用根域,而不是基于调用它的位置。在我的例子中subdomain1,你可以猜到这意味着会话包的不同存储。首先,这是一个不同的会话。

因此,存储在子域的会话中与存储在会话本身中是不同的。尽管如此,为了根据每个子域封装 Livewire 使用,我还需要进行其他自定义,图像我有一个基于多租户的项目,我需要这样的隔离。

标签: phplaravellaravel-livewirelivewires

解决方案


经过一番谷歌搜索并基于Livewire 论坛中的解决方案:

我将其定制为适合子域的使用

  • 第 1 步,添加新路线routes/app.php
use Livewire\Controllers\HttpConnectionHandler;

// .....
// Notice that this root route has to be the same as the one in step2:
Route::get('/', [AppController::class, 'index'])->name('app.index');
// .....

Route::post('livewire/message/{name}', [HttpConnectionHandler::class, '__invoke']);
  • Step2,在 Blade 之后@livewireScripts
@livewireScripts
<script>
    window.livewire_app_url = '{{route('app.index')}}';
</script>

如果您想知道 Livewire 的配置,则无需更改它。保持原样;null

'asset_url'  => null,

推荐阅读