Laravel7-检测到节流时在特定视图上重定向
当在登录期间检测到节流时,我需要将用户重定向到一个简单的信息视图 我有一个名为suspended.blade.php的视图 我已经定好了路线Laravel7-检测到节流时在特定视图上重定向,laravel,throttling,cartalyst-sentinel,Laravel,Throttling,Cartalyst Sentinel,当在登录期间检测到节流时,我需要将用户重定向到一个简单的信息视图 我有一个名为suspended.blade.php的视图 我已经定好了路线 Route::get('/suspended', function(){ return view('suspended'); }); 我正在使用Cartalyst/Sentinel。 在我的登录控制器中,我有如下内容: function LoginUser(Request $request){ // some validation stuf
Route::get('/suspended', function(){
return view('suspended');
});
我正在使用Cartalyst/Sentinel。
在我的登录控制器中,我有如下内容:
function LoginUser(Request $request){
// some validation stuff...
try {
$user = Sentinel::authenticate($request->all());
} catch (ThrottlingException $e) {
// user inserted too many times a wrong password
return redirect('/suspended');
} catch (NotActivatedgException $e) {
return redirect()->back()->with( ['error' => "Account not active yet."] );
}
// some other stuff...
}
如果我模仿长跑,我只会得到一个错误页面,而不是我的视图
为什么呢
谢谢
编辑
按照@PsyLogic的提示,我修改了我的函数如下:
function LoginUser(Request $request){
// some validation stuff...
try {
$user = Sentinel::authenticate($request->all());
}
/* remove this part to use the default behaviour described in app\Excpetions\Handler.php */
// catch (ThrottlingException $e) {
// return redirect('/suspended');
// }
catch (NotActivatedgException $e) {
return redirect()->back()->with( ['error' => "Account not active yet."]
);
}
// some other stuff...
}
仍然不工作,并显示带有所有调试代码的Laravel错误页面。Laravel已经有节流中间件,您可以扩展它并更新
handle()
方法
namespace App\Http\Middleware;
use Illuminate\Routing\Middleware\ThrottleRequests;
class CustomThrottleMiddleware extends ThrottleRequests
{
//...
}
并更新Handle.php文件中的新中间件
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
// ...
'throttle' =>App\Http\Middleware\CustomThrottleMiddleware::class,
]
或者,您可以保留原始索引油门并添加
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
// ...
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'custom_throttle' =>App\Http\Middleware\CustomThrottleMiddleware::class,
]
更新(简易方式)
如果这些更改不会影响您的软件包,那么让我们用简单的方法来实现,您可以更新App\Exceptions\Handler::class
中的render()
函数并进行测试
public function render($request, Throwable $exception)
{
if ($exception instanceof ThrottleRequestsException) {
return redirect()->route('suspended');
}
return parent::render($request, $exception);
}
谢谢@PsyLogic我不确定是否能够做到这一点,因为我不知道需要在CustomThrottleMiddle软件中放入什么。另外,此解决方案是否与Sentinel一起工作?我的项目就是基于此,所以我现在不能删除它。好的,读一下更新@SimoneContiThanks@PsyLogic,我觉得你的更新更好。我仍然对你的第一个解决方案感到好奇。我在互联网上搜索了一些东西,找到了这个,但我看不到Laravell handle()函数与我的情况之间的任何关系。没问题,我会测试你的解决方案。顺便说一句,如果您想逃避所有这些自定义更改(涉及多个文件),您可以针对一个具有一个条件的文件。不幸的是,它对我不起作用(请参阅我的编辑)。我仍然看到错误页面而不是我的视图。