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

Laravel安装Breeze后登录/仪表盘等路由返回404求助

Laravel 10 + Breeze 路由404问题

刚接触Laravel,在LAMP环境安装了Laravel 10(Windows和Ubuntu上Docker无法正常使用),欢迎页可正常加载。安装Breeze后,欢迎页右上角的登录、注册按钮点击后均返回404 Not Found,Breeze添加到routes/web.php中的/dashboard路由同样返回404。

已确认路由存在,routes/web.php已引入包含相关路由的auth.php,所有文件内容看起来均正常。以下是相关代码及路由列表:

routes/web.php

<?php

use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');

Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

require __DIR__.'/auth.php';

routes/auth.php

<?php

use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\Auth\ConfirmablePasswordController;
use App\Http\Controllers\Auth\EmailVerificationNotificationController;
use App\Http\Controllers\Auth\EmailVerificationPromptController;
use App\Http\Controllers\Auth\NewPasswordController;
use App\Http\Controllers\Auth\PasswordController;
use App\Http\Controllers\Auth\PasswordResetLinkController;
use App\Http\Controllers\Auth\RegisteredUserController;
use App\Http\Controllers\Auth\VerifyEmailController;
use Illuminate\Support\Facades\Route;

Route::middleware('guest')->group(function () {
    Route::get('register', [RegisteredUserController::class, 'create'])
                ->name('register');

    Route::post('register', [RegisteredUserController::class, 'store']);

    Route::get('login', [AuthenticatedSessionController::class, 'create'])
                ->name('login');

    Route::post('login', [AuthenticatedSessionController::class, 'store']);

    Route::get('forgot-password', [PasswordResetLinkController::class, 'create'])
                ->name('password.request');

    Route::post('forgot-password', [PasswordResetLinkController::class, 'store'])
                ->name('password.email');

    Route::get('reset-password/{token}', [NewPasswordController::class, 'create'])
                ->name('password.reset');

    Route::post('reset-password', [NewPasswordController::class, 'store'])
                ->name('password.store');
});

Route::middleware('auth')->group(function () {
    Route::get('verify-email', EmailVerificationPromptController::class)
                ->name('verification.notice');

    Route::get('verify-email/{id}/{hash}', VerifyEmailController::class)
                ->middleware(['signed', 'throttle:6,1'])
                ->name('verification.verify');

    Route::post('email/verification-notification', [EmailVerificationNotificationController::class, 'store'])
                ->middleware('throttle:6,1')
                ->name('verification.send');

    Route::get('confirm-password', [ConfirmablePasswordController::class, 'show'])
                ->name('password.confirm');

    Route::post('confirm-password', [ConfirmablePasswordController::class, 'store']);

    Route::put('password', [PasswordController::class, 'update'])->name('password.update');

    Route::post('logout', [AuthenticatedSessionController::class, 'destroy'])
                ->name('logout');
});

resources/views/welcome.blade.php(片段)

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel</title>

        <!-- Fonts -->
        <link rel="preconnect" href="https://fonts.bunny.net">
        <link href="https://fonts.bunny.net/css?family=figtree:400,600&display=swap" rel="stylesheet" />

    </head>
    <body class="antialiased">
        <div class="relative sm:flex sm:justify-center sm:items-center min-h-screen bg-dots-darker bg-center bg-gray-100 dark:bg-dots-lighter dark:bg-gray-900 selection:bg-red-500 selection:text-white">
            @if (Route::has('login'))
                <div class="sm:fixed sm:top-0 sm:right-0 p-6 text-right z-10">
                    @auth
                        <a href="{{ url('/dashboard') }}" class="font-semibold text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-white focus:outline focus:outline-2 focus:rounded-sm focus:outline-red-500">Dashboard</a>
                    @else
                        <a href="{{ route('login') }}" class="font-semibold text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-white focus:outline focus:outline-2 focus:rounded-sm focus:outline-red-500">Log in</a>

                        @if (Route::has('register'))
                            <a href="{{ route('register') }}" class="ml-4 font-semibold text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-white focus:outline focus:outline-2 focus:rounded-sm focus:outline-red-500">Register</a>
                        @endif
                    @endauth
                </div>
            @endif

views/auth/register.blade.php

<x-guest-layout>
    <form method="POST" action="{{ route('register') }}">
        @csrf

        <!-- Name -->
        <div>
            <x-input-label for="name" :value="__('Name')" />
            <x-text-input id="name" class="block mt-1 w-full" type="text" name="name" :value="old('name')" required autofocus autocomplete="name" />
            <x-input-error :messages="$errors->get('name')" class="mt-2" />
        </div>

        <!-- Email Address -->
        <div class="mt-4">
            <x-input-label for="email" :value="__('Email')" />
            <x-text-input id="email" class="block mt-1 w-full" type="email" name="email" :value="old('email')" required autocomplete="username" />
            <x-input-error :messages="$errors->get('email')" class="mt-2" />
        </div>

        <!-- Password -->
        <div class="mt-4">
            <x-input-label for="password" :value="__('Password')" />

            <x-text-input id="password" class="block mt-1 w-full"
                            type="password"
                            name="password"
                            required autocomplete="new-password" />

            <x-input-error :messages="$errors->get('password')" class="mt-2" />
        </div>

        <!-- Confirm Password -->
        <div class="mt-4">
            <x-input-label for="password_confirmation" :value="__('Confirm Password')" />

            <x-text-input id="password_confirmation" class="block mt-1 w-full"
                            type="password"
                            name="password_confirmation" required autocomplete="new-password" />

            <x-input-error :messages="$errors->get('password_confirmation')" class="mt-2" />
        </div>

        <div class="flex items-center justify-end mt-4">
            <a class="underline text-sm text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 rounded-md focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 dark:focus:ring-offset-gray-800" href="{{ route('login') }}">
                {{ __('Already registered?') }}
            </a>

            <x-primary-button class="ms-4">
                {{ __('Register') }}
            </x-primary-button>
        </div>
    </form>
</x-guest-layout>

php artisan route:list输出

GET|HEAD  / .......................................................................................................................................
POST      _ignition/execute-solution ................................ ignition.executeSolution › Spatie\LaravelIgnition › ExecuteSolutionController
GET|HEAD  _ignition/health-check ............................................ ignition.healthCheck › Spatie\LaravelIgnition › HealthCheckController
POST      _ignition/update-config ......................................... ignition.updateConfig › Spatie\LaravelIgnition › UpdateConfigController
GET|HEAD  api/user ................................................................................................................................
GET|HEAD  confirm-password ............................................................. password.confirm › Auth\ConfirmablePasswordController@show
POST      confirm-password ............................................................................... Auth\ConfirmablePasswordController@store
GET|HEAD  dashboard ..................................................................................................................... dashboard
POST      email/verification-notification .................................. verification.send › Auth\EmailVerificationNotificationController@store
GET|HEAD  forgot-password .............................................................. password.request › Auth\PasswordResetLinkController@create
POST      forgot-password ................................................................. password.email › Auth\PasswordResetLinkController@store
GET|HEAD  login ................................................................................ login › Auth\AuthenticatedSessionController@create
POST      login ......................................................................................... Auth\AuthenticatedSessionController@store
POST      logout ............................................................................. logout › Auth\AuthenticatedSessionController@destroy
PUT       password ............................................................................... password.update › Auth\PasswordController@update
GET|HEAD  profile ........................................................................................... profile.edit › ProfileController@edit
PATCH     profile ....................................................................................... profile.update › ProfileController@update
DELETE    profile ..................................................................................... profile.destroy › ProfileController@destroy
GET|HEAD  register ................................................................................ register › Auth\RegisteredUserController@create
POST      register ............................................................................................ Auth\RegisteredUserController@store
POST      reset-password ........................................................................ password.store › Auth\NewPasswordController@store
GET|HEAD  reset-password/{token} ............................................................... password.reset › Auth\NewPasswordController@create
GET|HEAD  sanctum/csrf-cookie ................................................... sanctum.csrf-cookie › Laravel\Sanctum › CsrfCookieController@show
GET|HEAD  verify-email ............................................................... verification.notice › Auth\EmailVerificationPromptController
GET|HEAD  verify-email/{id}/{hash} ............................................................... verification.verify › Auth\VerifyEmailController

                                                                                                                              Showing [25] routes

解决建议

  • 启用Apache重写模块:
    Ubuntu系统执行sudo a2enmod rewrite,然后重启Apache:sudo systemctl restart apache2;Windows系统打开Apache的httpd.conf文件,找到#LoadModule rewrite_module modules/mod_rewrite.so,移除开头的#,保存后重启Apache服务。
  • 配置AllowOverride为All:
    编辑站点的虚拟主机配置文件,确保项目public目录的AllowOverride设置为All,示例:
    <Directory /var/www/your-project/public>
        AllowOverride All
    </Directory>
    
    修改后重启Apache生效。
  • 确认站点根目录指向public文件夹:
    LAMP环境下,必须将站点的根目录设置为Laravel项目的public文件夹,而非项目根目录,否则路由无法被Laravel的前端控制器正确解析。
  • 检查public/.htaccess文件:
    确保该文件存在且内容正确,默认内容如下,若缺失可直接创建:
    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews -Indexes
        </IfModule>
    
        RewriteEngine On
    
        # Handle Authorization Header
        RewriteCond %{HTTP:Authorization} .
        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
        # Redirect Trailing Slashes If Not A Folder...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} (.+)/$
        RewriteRule ^ %1 [L,R=301]
    
        # Send Requests To Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    </IfModule>
    
  • 清除路由缓存:
    在项目根目录执行php artisan route:clear,确保路由配置是最新的。
  • 校验APP_URL配置:
    打开.env文件,确认APP_URL与你的实际访问地址一致,比如http://localhost/your-project,避免生成的路由链接错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 23:04:51