我正在我的Laravel应用程序中试验中间件.目前,我已经将其设置为在每个路由上为经过身份验证的用户运行,但是,我希望它忽略任何以setup
URI开头的请求.
Here is what my CheckOnboarding
middleware method looks like:
public function handle($request, Closure $next)
{
/**
* Check to see if the user has completed the onboarding, if not redirect.
* Also checks that the requested URI isn't the setup route to ensure there isn't a redirect loop.
*/
if ($request->user()->onboarding_complete == false && $request->path() != 'setup') {
return redirect('setup');
} else {
return $next($request);
}
}
这在我的路由中使用如下:
Route::group(['middleware' => ['auth','checkOnboarding']], function () {
Route::get('/home', 'HomeController@index');
Route::get('/account', 'AccountController@index');
Route::group(['prefix' => 'setup'], function () {
Route::get('/', 'OnboardingController@index')->name('setup');
Route::post('/settings', 'SettingsController@store');
});
});
Now, if I go to /home
or /account
I get redirected to /setup
as you would expect. This originally caused a redirect loop error hence why & $request->path() != 'setup'
is in the Middleware.
I feel like this is a really clunky way of doing it, and obviously doesn't match anything after setup
like the setup/settings
route I have created.
有没有更好的方法可以让这个中间件为用户在所有路由上运行,但也可以设置某些应该免于此判断的路由?