Laravel Livewire 3升级后rappasoft/laravel-livewire-tables资源路径404问题及配置方案咨询
看起来你已经搞定了Livewire核心脚本的路径问题,但rappasoft表格组件的资源还没适配子目录部署的情况,我给你几个可行的解决方案:
先确认基础前提:发布包资源
首先得确保你已经把rappasoft包的静态资源发布到项目的public目录,执行这条命令:
php artisan vendor:publish --tag=livewire-tables:assets
执行后,资源会被放到public/vendor/rappasoft/laravel-livewire-tables目录下,这是后续配置的基础。
方案一:配置Laravel全局资源前缀(最简单)
你的项目部署在子目录/example/public下,直接在.env文件里设置全局资源URL前缀,让Laravel的asset()函数自动带上子目录路径:
ASSET_URL=http://xxx.xxx.x.xxx/example/public
如果.env设置没生效,也可以直接修改config/app.php里的配置:
'asset_url' => env('ASSET_URL', 'http://xxx.xxx.x.xxx/example/public'),
这样rappasoft包生成资源路径时,会自动套用这个前缀,资源请求就会指向正确的子目录位置了。
方案二:手动注册路由映射资源(灵活适配)
要是全局配置没起作用,你可以像处理Livewire脚本那样,手动注册路由来映射rappasoft的资源请求,把根路径的/rappasoft/...请求转发到实际的资源目录:
在routes/web.php中添加以下代码:
// 处理rappasoft表格组件的静态资源请求 Route::get('/rappasoft/laravel-livewire-tables/{file}', function ($file) { // 拼接实际的资源文件路径 $resourcePath = public_path("vendor/rappasoft/laravel-livewire-tables/{$file}"); // 文件不存在则返回404 if (!file_exists($resourcePath)) { abort(404); } // 返回文件响应 return response()->file($resourcePath); })->where('file', '.*');
这个方法相当于做了个URL重定向,让错误的请求路径指向正确的资源位置。
方案三:关闭自动注入,手动引入资源(完全可控)
如果上面两种方法都不适用,你可以关闭包的自动资源注入,手动在布局文件里指定正确的资源路径:
- 先修改
config/livewire-tables.php里的配置,关闭自动注入:
'inject_core_assets_enabled' => false, 'inject_third_party_assets_enabled' => false,
- 在你的主布局文件(比如
resources/views/layouts/app.blade.php)中,手动引入这些资源:
<!-- 引入rappasoft表格核心样式 --> <link rel="stylesheet" href="{{ asset('vendor/rappasoft/laravel-livewire-tables/core.min.css') }}"> <link rel="stylesheet" href="{{ asset('vendor/rappasoft/laravel-livewire-tables/thirdparty.css') }}"> <!-- 引入rappasoft表格核心脚本 --> <script src="{{ asset('vendor/rappasoft/laravel-livewire-tables/core.min.js') }}"></script> <script src="{{ asset('vendor/rappasoft/laravel-livewire-tables/thirdparty.min.js') }}"></script>
这种方式能完全掌控资源加载路径,避免包自动生成路径的问题。
另外补充一点:确保Livewire 3的资源路径配置完整,除了设置脚本路由,还可以加上资源URL配置,在routes/web.php中添加:
Livewire::setAssetUrl('/example/public/livewire');
这样Livewire的其他相关资源也会使用正确的子目录路径。
备注:内容来源于stack exchange,提问作者Petitemo

