gpt4 book ai didi

php - Laravel:自定义登录重定向到/home 而不是/welcome 页面

转载 作者:行者123 更新时间:2023-12-04 15:40:20 25 4
gpt4 key购买 nike

我正在尝试创建自定义登录。登录后,它登陆到“/home”页面而不是“/welcome”页面。

这是我的登录 Controller :

class LoginController extends Controller
{
use AuthenticatesUsers;

protected $redirectTo = '/welcome';

public function __construct()
{
$this->middleware('guest')->except('logout');
}

public function view()
{
return view('auth.login');
}

public function login(Request $request)
{
$this->validate($request, [
'email' => 'required',
'password' => 'required',
]);

$loginValue = $request->input('email');

$login_type = $this->getLoginType($loginValue);

$request->merge([
$login_type => $loginValue
]);

if (auth()->attempt($request->only($login_type, 'password'))) {
// return redirect()->intended($this->redirectPath());
return redirect()->intended(route('welcome'));
}
return redirect()->back()->withInput()->withErrors([ 'email' => "These credentials do not match our records." ]);
}

public function getLoginType($loginValue)
{
return filter_var($loginValue, FILTER_VALIDATE_EMAIL ) ? 'email'
: ( (preg_match('%^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \/]?)?((?:\(?\d{1,}\)?[\-\.\ \/]?){0,})(?:[\-\.\ \/]?(?:#|ext\.?|extension|x)[\-\.\ \/]?(\d+))?$%i', $loginValue)) ? 'mobile' : 'name' );
}
}

我更改了 Illuminate\Foundation\Exception\Handler.php 的重定向路径:

protected function unauthenticated($request, AuthenticationException $exception)
{
return $request->expectsJson() ? response()->json(['message' => $exception->getMessage()], 401 : redirect('/');
}

我的路线文件:

Route::get('/login', 'Auth\LoginController@view')->name('login');
Route::post('/login', 'Auth\LoginController@login');

最佳答案

如果您想重定向到“/欢迎”页面。您应该要求更改中间件。

中间件:RedirectIfAuthenticated.php

需要进行以下更改。

public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/welcome');
}
return $next($request);
}

将返回重定向更改为“/welcome”而不是“/home”。

我希望这个解决方案可以帮助解决您的问题。谢谢。

关于php - Laravel:自定义登录重定向到/home 而不是/welcome 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58004998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com