我正在我的 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
路线不匹配。
有没有更好的方法让这个中间件在用户的所有路由上运行,但也设置某些应该免于此检查的路由?
您所做的没有任何问题,但是,我建议将您的路线组分开,即:
Route::group(['middleware' => ['auth', 'checkOnboarding']], function () {
Route::get('/home', 'HomeController@index');
Route::get('/account', 'AccountController@index');
});
Route::group(['prefix' => 'setup', 'middleware' => 'auth'], function () {
Route::get('/', 'OnboardingController@index')->name('setup');
Route::post('/settings', 'SettingsController@store');
});
或者,为您的身份验证设置一个父组:
Route::group(['middleware' => 'auth'], function () {
Route::group(['middleware' => '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');
});
});
这也意味着您可以删除中间件中的额外条件:
/**
* 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.
*/
return $request->user()->onboarding_complete ? $next($request) : redirect('setup');
希望这可以帮助!
您可以使用 Controller 类来获得非常壮观的结果。
正确答案
如果您在 HTTP/Controllers/Controller.php 中创建一个 __construct 函数,那么您可以声明中间件以在每个控制器操作上运行,甚至可以根据需要声明异常。
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function __construct(){
$this->middleware('auth',['except' => ['login','setup','setupSomethingElse']]);
}
}
请注意不要将任何标准的索引、存储、更新、销毁功能放在异常中,否则您将打开潜在的安全问题。
从 Laravel 7.7 开始,您可以excluded_middleware
像这样使用:
Route::group(['middleware' => ['auth','checkOnboarding']], function () {
Route::get('/home', 'HomeController@index');
Route::get('/account', 'AccountController@index');
Route::group([
'prefix' => 'setup',
'excluded_middleware' => ['checkOnboarding'],
], function () {
Route::get('/', 'OnboardingController@index')->name('setup');
Route::post('/settings', 'SettingsController@store');
});
});
有两种方法可以解决这个问题
尝试在路线文件中筛选您的路线
web.php or api.php
跳过路线
middleware
对于全局中间件(您希望在所有路由之前运行的中间件),您应该在中间件中跳过路由。
例如:
//add an array of routes to skip santize check
protected $openRoutes = [
'setup/*',
];
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(!in_array($request->path(), $this->openRoutes)){
//middleware code or call of function
}
return $next($request);
}
对于其他中间件,您可以轻松跳过路由文件并根据中间件对路由进行分组。
例如:
Route::group(['middleware' => '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');
});
所有路由上的中间件,除了一条
目前在我的中间件内核中有以下内容:
protected $middleware = [ ... ... ... ... \App\Http\Middleware\exists::class, ];
以上将中间件“存在”应用于每个路由请求。有没有一种方法可以使它适用于除一个之外的每个路由请求?