【发布时间】:2017-08-25 09:33:32
【问题描述】:
我正在我的 Laravel 应用程序中试验中间件。我目前已将它设置为在经过身份验证的用户的每条路由上运行,但是,我希望它忽略任何以 setup URI 开头的请求。
这是我的CheckOnboarding 中间件方法的样子:
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');
});
});
现在,如果我转到 /home 或 /account,我会按照您的预期重定向到 /setup。这最初导致了重定向循环错误,因此& $request->path() != 'setup' 在中间件中。
我觉得这是一种非常笨拙的方式,显然与 setup 之后的任何东西都不匹配,比如我创建的 setup/settings 路由。
有没有更好的方法让这个中间件在用户的所有路由上运行,同时还设置一些应该免除此检查的路由?
【问题讨论】:
标签: php laravel laravel-5 laravel-5.4 laravel-middleware