Laravel登录后出现Too many redirects错误,求助解决房源添加页跳转问题
Hey there, let's figure out why you're stuck in this redirect loop and fix it up—this is a common gotcha in Laravel, so we'll get you sorted quickly.
First, let's diagnose the root cause
That "too many redirects" error almost always means your app is bouncing between two pages infinitely. In your case, it's likely either:
- Your login route is accidentally protected by authentication (so unlogged users get sent to login... which requires login, creating a loop),
- Your custom redirect middleware is misconfigured and sending users back to the login page after they log in,
- Or your manual
Auth::check()logic in thecreatemethod is conflicting with other middleware.
Let's fix this step by step
1. Make sure your login route isn't locked down
First things first: your login page should be accessible to unauthenticated users. Double-check your routes/web.php—if you've added the auth middleware to your login routes, that's a guaranteed loop.
Your login routes should look like this (no auth middleware attached):
// Correct: Login routes are open to everyone Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login'); Route::post('/login', [AuthController::class, 'login']);
If you see ->middleware('auth') on either of these, remove it immediately—that's probably the main culprit.
2. Ditch the manual login check in create() and use Laravel's built-in middleware
Your current create method has a manual Auth::check() check, but Laravel's built-in auth middleware handles this way more reliably, including automatic redirects back to the intended page after login.
Here's how to refactor:
- Update your route in
routes/web.phpto protect it withauth:Route::get('/properties/create', [PropertiesController::class, 'create']) ->name('properties.create') ->middleware('auth'); - Simplify your
createmethod inPropertiesController—no more manual login checks:public function create() { $property_types = PropertyType::all(); return view('frontend.properties.create', ['property_types' => $property_types]); }
Laravel will automatically send unlogged users to the login page, and once they log in, it'll bounce them right back to /properties/create without any extra code from you. This is the cleanest, most maintainable solution.
3. If you must use a custom redirect middleware, fix its logic
If you need to keep your custom middleware for some reason, make sure it doesn't record the login page as the "intended" URL. Here's a corrected version:
public function handle(Request $request, Closure $next) { // Only save the intended URL if the user is NOT logged in, // and they're NOT already on the login page if (!Auth::check() && $request->route()->getName() !== 'login') { session()->put('url.intended', $request->fullUrl()); } return $next($request); }
Then, in your AuthController's login method, make sure you redirect to the intended URL (or a fallback) after successful login:
public function login(Request $request) { // Your existing validation & auth attempt logic here... if (Auth::attempt($credentials)) { $request->session()->regenerate(); // Redirect to the saved intended page, or fallback to create page return redirect()->intended(route('properties.create')); } // Login failure logic here... }
This ensures you never loop back to the login page after authentication.
4. Clean up old session data
Sometimes stale session values can cause weird loops. Run this command in your terminal to clear Laravel's session store:
php artisan session:clear
Also, clear your browser's cache and cookies for localhost to be safe.
Final note
Using Laravel's built-in auth middleware is almost always the best approach here—it's battle-tested and handles all the edge cases you might miss with custom code.
内容的提问来源于stack exchange,提问作者Purushotam Sangroula

