gpt4 book ai didi

Laravel 重定向在事件处理程序/监听器中不起作用

转载 作者:行者123 更新时间:2023-12-01 02:24:24 25 4
gpt4 key购买 nike

我有一个 Auth.Attempt 事件处理程序类,我检测用户的登录尝试来决定锁定用户的帐户。
但是,当我尝试使用 flash 消息将用户重定向到登录页面时,我发现重定向不起作用,它仍在进行下一步。
我想在事件中中断过程并给出我的自定义警告消息。谁能帮我吗?非常感谢。

我的事件处理程序:

namespace MyApp\Handlers\Security;

use DB;
use Session;
use Redirect;

class LoginHandler
{
/**
* Maximum attempts
* If user tries to login but failed more than this number, User account will be locked
*
* @var integer
*/
private $max_attemtps;

/**
* Maximum attempts per IP
* If an IP / Device tries to login but failed more than this number, the IP will be blocked
*
* @var integer
*/
private $ip_max_attempts;

public function __construct()
{
$this->max_attempts = 10;
$this->ip_max_attempts = 5;
}

public function onLoginAttempt($data)
{
//detection process.......
// if login attempts more than max attempts
return Redirect::to('/')->with('message', 'Your account has been locked.');
}
}

现在我这样做的方式如下:
Session::flash('message', 'Your account has been locked.');
header('Location: '.URL::to('/'));

它有效,但我不确定这是否是完美的方法。

最佳答案

不太深入这个非常有趣的讨论:

Should exceptions be used for flow control

您可以尝试设置自己的异常处理程序并从那里重定向到登录页面。

class FancyException extends Exception {}

App::error(function(FancyException $e, $code, $fromConsole)
{
$msg = $e->getMessage();
Log::error($msg);

if ( $fromConsole )
{
return 'Error '.$code.': '.$msg."\n";
}

if (Config::get('app.debug') == false) {
return Redirect::route('your.login.route');
}
else
{
//some debug stuff here
}


});

在你的功能中:
public function onLoginAttempt($data)
{
//detection process.......
// if login attempts more than max attempts
throw new FancyException("some msg here");
}

关于Laravel 重定向在事件处理程序/监听器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17797930/

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