Laravel 5.6.3修改登录重定向URL问题求助
Hey there, let's fix that redirect issue for your admin panel. The problem occurs because Laravel's default unauthenticated handler points to /login, but we need to override that to send users to /admin/login instead. Let's break this down based on your Laravel version since you mentioned hitting errors with the 5.5 approach:
In these versions, you'll need to override the unauthenticated method in your app/Exceptions/Handler.php file. Here's the correct way to do it:
- First, import the required exception class at the top of the file:
use Illuminate\Auth\AuthenticationException;
- Then, add or modify the
unauthenticatedmethod to target admin routes specifically:
protected function unauthenticated($request, AuthenticationException $exception) { // Redirect admin-related unauthenticated requests to admin login if ($request->is('admin/*') || $request->path() === 'admin') { return redirect()->guest('/admin/login'); } // Keep default redirect for non-admin routes return redirect()->guest(route('login')); }
This ensures any unauthenticated visit to /admin or its subroutes gets sent to your desired login page instead of the default.
Starting from Laravel 5.8, the handling of unauthenticated redirects evolved. You have two reliable options here:
Option 1: Use Custom Guards (Recommended for Multi-Auth Setups)
If you've set up a dedicated admin guard in config/auth.php, you can redirect based on the failing guard:
In app/Exceptions/Handler.php, add this modified render method (or override unauthenticated):
use Illuminate\Auth\AuthenticationException; public function render($request, Throwable $exception) { if ($exception instanceof AuthenticationException) { $guard = array_get($exception->guards(), 0); switch ($guard) { case 'admin': return redirect()->guest('/admin/login'); default: return redirect()->guest('/login'); } } return parent::render($request, $exception); }
Just make sure your admin routes are using the auth:admin middleware to trigger this guard check.
Option 2: Path-Based Redirect (No Custom Guard Needed)
If you don't use separate guards, you can still check the request path like we did in older versions:
use Illuminate\Auth\AuthenticationException; protected function unauthenticated($request, AuthenticationException $exception) { return $request->expectsJson() ? response()->json(['message' => $exception->getMessage()], 401) : redirect()->guest($request->is('admin/*') || $request->path() === 'admin' ? '/admin/login' : '/login'); }
- Missing Import: Forgetting to include
use Illuminate\Auth\AuthenticationException;at the top ofHandler.phpis a top reason the 5.5 approach throws errors. - Incorrect Route Matching: Double-check that your path conditions match your admin routes.
$request->is('admin/*')catches all subroutes, while$request->path() === 'admin'targets the root/adminpage. - Guard Misconfiguration: If using a custom admin guard, confirm it's properly set up in
config/auth.php(providers, driver, etc.) and linked to your admin routes via middleware.
That should get your unauthenticated admin requests redirecting to /admin/login as expected!
内容的提问来源于stack exchange,提问作者Attila Naghi

