You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Livewire 3点击按钮出现404及请求错误问题求助

Laravel Livewire 3 点击按钮出现404错误的解决办法

以下是针对POST http://localhost/livewire/update 404 (Not Found)错误的排查和解决步骤:

  • 清理路由与视图缓存
    Livewire的自动注册路由可能因缓存失效,执行以下命令清除缓存:

    php artisan route:clear
    php artisan view:clear
    
  • 修正脚本与样式加载位置
    Livewire 3对资源加载位置有严格要求:

    • @livewireStyles必须放在页面<head>标签内,确保你的母版页中@yield('custom-css')的位置在<head>里
    • @livewireScripts必须放在</body>标签之前,确保母版页中@yield('custom-js')的位置在页面底部
  • 验证组件文件与命名
    确保Counter组件的PHP文件和视图文件位置、命名正确:

    • PHP组件文件app/Http/Livewire/Counter.php:
      namespace App\Http\Livewire;
      
      use Livewire\Component;
      
      class Counter extends Component
      {
          public $count = 0;
      
          public function increment()
          {
              $this->count++;
          }
      
          public function decrement()
          {
              $this->count--;
          }
      
          public function render()
          {
              return view('livewire.counter');
          }
      }
      
    • 视图文件resources/views/livewire/counter.blade.php(你的现有视图代码没问题)
  • 检查CSRF令牌配置
    确保母版页的<head>中包含CSRF令牌元标签:

    <meta name="csrf-token" content="{{ csrf_token() }}">
    

    Livewire依赖CSRF令牌验证请求,缺失会导致请求异常。

  • 重新发布Livewire静态资源
    执行命令发布Livewire的JS和CSS资产,确保资源文件存在:

    php artisan livewire:publish --assets
    

内容的提问来源于stack exchange,提问作者Enad Abuzaid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 18:15:55