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

Laravel登录后未跳转至Home页面问题求助

Laravel登录功能异常修复方案

问题根源分析

  1. 登录请求无返回响应:LoginController的login方法中,验证失败分支仅设置了Session闪存消息,但未返回任何响应,导致POST路由直接输出空白页面;
  2. 错误提示未渲染:登录视图login.blade.php未读取并显示Session中的错误闪存消息;
  3. Session存储错误:登录成功时,session(['username' => 'username'])是硬编码字符串,未实际存储用户的用户名。

修复步骤

1. 修复LoginController的login方法

确保无论验证成功或失败,都返回响应:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Redirect;

class LoginController extends Controller
{
    public function index()
    {
        return view('login');         
    }
   
    public function login(Request $request)
    {
        $username = $request->input('username');
        $password = $request->input('password');

        if(Auth::attempt(['username' => $username, 'password' => $password, 'active' => 1])) {
            $request->session()->regenerate();
            // 存储实际的用户名到session
            session(['username' => $username]);
            return redirect()->route('home');
        } else {
            // 添加with方法传递闪存消息,同时重定向回登录页
            return redirect()->route('login')->with('alert-error', '用户名或密码错误,请重试');
        }
    }

    public function logout(Request $request)
    {
        Auth::logout();
        $request->session()->invalidate();
        $request->session()->regenerateToken();

        return Redirect::route('login');
    }
}

2. 在登录视图中添加错误提示渲染

修改login.blade.php,在表单上方添加闪存消息显示区域:

<!DOCTYPE html>
<html>
    @include('layout.header')
     <!-- Custom Styles -->
  <link href="{{asset('css/stylesheet.css')}}" rel="stylesheet">
    <body style="text-align:center;">
<br><br><br><br>

       <div class="form">
            <h2>LOGIN</h2>
            <p>Please enter your credentials to login.</p>
            
            <!-- 新增:显示错误提示 -->
            @if(session('alert-error'))
                <div style="color: red; margin-bottom: 15px;">
                    {{ session('alert-error') }}
                </div>
            @endif
        
        <form action="{{route('login.post')}}" method="POST" class="form-group">
        {{ csrf_field() }}
          <input type="text" name="username" id="username" placeholder="Username" class="form-control" required/><br><br>
          <input type="password" name="password" id="password" placeholder="Password" class="form-control" required/><br><br>
          <button type="submit" name="login" id="login" class="btn btn-primary">login</button><br><br>
          <a style="color:black;float:left;" href=" " >Change Password</a>
        </form>
</div>
    </body>
</html>

3. 可选:优化路由安全性

确保home路由受auth中间件保护,防止未登录用户直接访问:

// web.php
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home')->middleware('auth');

修复说明

  • 验证失败时,通过redirect()->route('login')->with(...)重定向回登录页并携带闪存消息,彻底解决空白页问题;
  • 登录视图中通过@if(session('alert-error'))判断并显示错误信息,实现错误提示功能;
  • 修正Session中用户名的存储逻辑,确保存储的是用户输入的实际用户名;
  • 为home路由添加auth中间件,避免未登录用户直接进入首页。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 10:42:52