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

Laravel项目在Apache正常,php artisan serve与Vite报404错误求助

问题解决:Laravel在不同运行环境下的404问题

核心原因

你的表单提交路径使用了硬编码的绝对路径/vite_experiment/public/register,这个路径仅适配当前XAMPP的站点配置,但php artisan serve和Vite开发服务器的根目录是Laravel项目的public文件夹,导致路径不匹配触发404。

解决方案

步骤1:优化路由(可选但推荐)

给路由添加名称,方便后续维护:

// routes/web.php
use App\Http\Controllers\UserController;

Route::post('/register', [UserController::class, 'register'])->name('register');

步骤2:修正表单提交路径

将home.blade.php中的表单action改为Laravel提供的路由辅助函数生成的路径,避免硬编码:

<!-- views/home.blade.php -->
<body>
    <h1>register</h1>

    <form action="{{ route('register') }}" method="POST">
        @csrf
        <input type="text" placeholder="user">
        <input type="text" placeholder="email">
        <input type="password" placeholder="password">
        <button>register</button>
    </form>
</body>

如果不想给路由命名,也可以使用url()辅助函数:

<form action="{{ url('/register') }}" method="POST">

环境差异说明

  • XAMPP Apache:你应该是将项目根目录(vite_experiment)设为了站点根,所以访问路径为http://localhost/vite_experiment/public/register,和硬编码路径匹配,因此能正常运行。
  • php artisan serve:默认将public文件夹作为服务器根目录,访问地址为http://localhost:8000/,硬编码的/vite_experiment/public/register会指向不存在的路径,导致404。
  • Vite开发服务器:同样以public为根目录提供资源,硬编码路径无法匹配到正确的路由,因此显示页面找不到。

额外排查点

确认routes/web.php顶部已经正确引入了UserController:

use App\Http\Controllers\UserController;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 04:18:10