[英]Laravel 5, why is a redirect to named route in middleware causing “localhost redirected you too many times”
I have a pretty straight forward middleware: 我有一个非常简单的中间件:
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);
}
This causes the redirect loop. 这会导致重定向循环。 I am only calling the middleware via Kernel.php.
我只通过Kernel.php调用中间件。
My Kernal.php: 我的Kernal.php:
<?php namespace App\Http;
use Illuminate\\Foundation\\Http\\Kernel as HttpKernel; 使用Illuminate \\ Foundation \\ Http \\ Kernel作为HttpKernel;
class Kernel extends HttpKernel { 类内核扩展HttpKernel {
/**
* The application's global HTTP middleware stack.
*
* @var array
*/
protected $middleware = [
'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
'Illuminate\Cookie\Middleware\EncryptCookies',
'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
'Illuminate\Session\Middleware\StartSession',
'Illuminate\View\Middleware\ShareErrorsFromSession',
'App\Http\Middleware\VerifyCsrfToken',
'App\Http\Middleware\FeesOwed'
];
/**
* The application's route middleware.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => 'App\Http\Middleware\Authenticate',
'auth.signed' => 'App\Http\Middleware\AuthenticateSigned',
'fees' => 'App\Http\Middleware\FeesOwed',
'auth.subscribed' => 'App\Http\Middleware\AuthenticateSubscribed',
'admin' => 'App\Http\Middleware\AuthenticateAdmin',
'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
];
} }
thanks in advance. 提前致谢。
You need to apply that middleware to all routes but profile.investment.fees
. 您需要将该中间件应用于除
profile.investment.fees
所有路由。 In your kernel, add your middleware in the $routeMiddleware
array as 在您的内核中,将中间件添加到
$routeMiddleware
数组中,如下所示:
'alias' => \App\Http\Middleware\MyMiddleware::class,
Then in your route define a group containing that middleware, and make sure profile.investment-fees
is out of it 然后在您的路由中定义一个包含该中间件的组,并确保没有
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
});
Alternatively, in your middleware, you could simply avoid that specific route by ignoring it with an if else 另外,在您的中间件中,您可以通过使用if else忽略该特定路由来简单地避免它
public function handle(Request $request, Closure $next) {
if ($request->is('pif')) {
return $next($request);
}
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.