【发布时间】:2017-08-13 20:12:03
【问题描述】:
我有一个非常简单的中间件:
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 调用中间件。
我的内核.php:
<?php namespace App\Http;
使用 Illuminate\Foundation\Http\Kernel 作为 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',
];
}
提前致谢。
【问题讨论】:
-
你的路线是什么样的?此中间件不适用于
profile.investment-fees路由,对吧? -
不是我没有将它应用于路由,只是将它作为中间件添加到 Kernal.php 中
-
你的问题。如果这适用于每条路线,那么它也将适用于
profile.investment-fees。当您尝试重定向到该路由时,它会尝试再次重定向,因为pastDueFees()没有更改 -
确保只将中间件添加到
$routeMiddleware而不是$middlewareGroups或$middleware -
有多种方法可以做到这一点。如果当前页面是 profile.investment-fees 页面,您可以在中间件中进行额外检查以不重定向。如果这意味着在每一页上,那可能是最好的。否则,您可以将其更改为像 EddyTheDoves answer 这样的路由中间件