Php Laravel 5,为什么在中间件中重定向到命名路由会导致“localhost重定向您太多次”
我有一个非常直截了当的想法:Php Laravel 5,为什么在中间件中重定向到命名路由会导致“localhost重定向您太多次”,php,laravel,Php,Laravel,我有一个非常直截了当的想法: protected $auth; public function __construct(Guard $auth) { $this->auth = $auth; } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public func
protected $auth;
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
//dd($this->auth->user());
if($this->auth->user()->id && $this->auth->user()->pastDueFees()){
\Session::flash('message','You must pay past due deal fees before using the rest of the website');
return redirect()->route('profile.investment-fees');
}
return $next($request);
}
这会导致重定向循环。我只是通过Kernel.php调用中间件
My Kernal.php:
<?php namespace App\Http;
}
提前感谢。您需要将该中间件应用于除profile.investment.fees之外的所有路由。在内核中,将中间件添加到$routeMiddleware数组中,如下所示:
'alias' => \App\Http\Middleware\MyMiddleware::class,
然后在您的路径中定义一个包含该中间件的组,并确保profile.investment-fees不在其中
Route::get('pif', 'MyController@pif')->name('profile.investment-fees');
//Route group
Route::group(['middleware' => 'alias'], function(){
//every other routes that need the middleware
});
或者,在您的中间件中,您可以通过使用if-else忽略该特定路由来避免该路由
public function handle(Request $request, Closure $next) {
if ($request->is('pif')) {
return $next($request);
}
...
}
你的路线是什么样的?这个中间件不适用于概要文件。投资费用路由,对吗?不,我不适用于路由,只是将它作为中间件添加到内核中。php这是你的问题。如果这适用于每条路线,那么它也将适用于profile.investment-fees。当您尝试重定向到该路由时,它会再次尝试重定向,因为过去的DueFees没有更改。请确保您只将中间件添加到$routeMiddleware,而不是$middlewareGroups或$middlewareThere。有多种方法可以做到这一点。如果当前页面是profile.investment-fees页面,您可以在中间件中进行额外检查,以避免重定向。如果每一页都有这样的内容,那就最好了。否则,您可以将其更改为EddyTheDoves answer之类的路由中间件
public function handle(Request $request, Closure $next) {
if ($request->is('pif')) {
return $next($request);
}
...
}