gpt4 book ai didi

php - 获取预期的目标 url 而不会丢失中间件中的值

转载 作者:行者123 更新时间:2023-12-03 23:38:18 25 4
gpt4 key购买 nike

我有以下两个设置页面路由,一个用于正常设置,另一个用于安全和管理相关设置,它使用相同的中间件“password.confirm”

Route::get('/admin/settings',WebsiteInfoController::class,'edit'])->name('settings')->middleware('password.confirm');

Route::get('/settings',[WebsiteInfoController::class, edit'])->name('user.settings')->middleware('password.confirm');

这个中间件将我重定向到第二个页面,我必须在其中输入密码,然后我才能访问我想要的页面。在我的中间件中,我有以下功能。我想再检查一下用户是否打算访问与管理员相关的设置

public function store(Request $request)
{
$this->validate($request, [
'secret_password' => ['sometimes','required'],
'password' => ['required','password:web'],
]);
if(redirect()->intended()->getTargetUrl()==route('settings')){
$secret_password =WebsiteInfo::first()->secret_password;
if (!Hash::check($request->secret_password, $secret_password)) {
throw ValidationException::withMessages([
'secret_password' => __('auth.password'),
]);
}
}
$request->session()->put('auth.password_confirmed_at', time());
return redirect()->intended(RouteServiceProvider::HOME);
});
});
}

在此方法中一切正常,但在执行所有检查后预期的 URL 丢失,我被重定向到主页而不是设置页面。我还尝试将 URL 保存在变量中,稍后在重定向命令中使用它,例如

$path=redirect()->intended()->getTargetUrl();

if($path==route('settings')){
$secret_password =WebsiteInfo::first()->secret_password;
if (!Hash::check($request->secret_password, $secret_password)) {
throw ValidationException::withMessages([
'secret_password' => __('auth.password'),
]);
}
}
$request->session()->put('auth.password_confirmed_at', time());
return redirect()->intended($path);

此方法工作正常,但如果第二次验证失败并且用户被重定向回确认密码页面,它也会丢失 URL。现在,当我第二次尝试执行验证时,它再次丢失了预期的 URL,并将我重定向回主页。我还尝试使用 $request 方法进行检查。

 if($request->route()->named('settings')){
$secret_password =WebsiteInfo::first()->secret_password;
if (!Hash::check($request->secret_password, $secret_password)) {
throw ValidationException::withMessages([
'secret_password' => __('auth.password'),
]);
}
}

但是,此方法无法检测中间件中的路由,并且根本不会执行验证检查。

所以,我的问题是如何检查预期的 URL 并执行验证检查,而不会在多次验证尝试失败后丢失预期的 URL?

最佳答案

你的方法没问题。您只是使用了错误的方法来提取目标网站 URL。 redirect()->intended()->getTargetUrl() 确实为您提供了目标页面 URL,但它也从 session 中删除了目标网站 URL,因此当您完成检查并想要要重定向到预期页面,在 session 中找不到预期的页面 URL,您将被重定向到默认的回退 URL。这就是重定向函数的作用

public function intended($default = '/', $status = 302, $headers = [], $secure = null) {
$path = $this->session->pull('url.intended', $default);

return $this->to($path, $status, $headers, $secure);
}

在这里,$request->route()->named('settings) 方法不起作用,因为您没有直接与初始 View 交互,而是通过中间件 View 交互发送预期的页面请求。使用以下代码,我想您的验证尝试会很好。即使在多次登录尝试失败后,它也能正常工作。

public function store(Request $request) {
$this->validate($request, [
'secret_password' => ['sometimes','required'],
'password' => ['required','password:web'],
]);
$path=session()->get('url.intended', RouteServiceProvider::HOME);
if($path==route('settings')) {
$secret_password =WebsiteInfo::first()->secret_password;
if (!Hash::check($request->secret_password, $secret_password)) {
throw ValidationException::withMessages([
'secret_password' => __('auth.password'),
]);
}
}
$request->session()->put('auth.password_confirmed_at', time());
return redirect()->intended($path);
}

关于php - 获取预期的目标 url 而不会丢失中间件中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68082636/

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