Laravel 5.8 直接添加多个中间件到路由

发布于 2025-01-10 08:28:55 字数 588 浏览 5 评论 0原文

我正在使用 Laravel 5.8,我有一个这样的路线:

Route::get("certs","CertController@index")->name('certificate.front')->middleware('auth');

现在我想向该路线添加另一个中间件,所以我尝试了这个:

Route::get("certs", "CertController@index")->name('certificate.front')->middleware('prevent-back-history','auth');

现在我没有收到任何错误并且它可以工作但我想知道这种方式是否更好:

Route::get("certs", "CertController@index")->name('certificate.front')->middleware(['prevent-back-history','auth']);

那么在这种情况下哪个更好、更正确?

请注意,我不想使用路由组,并且需要直接向路由指定中间件名称。

I'm using Laravel 5.8 and I have a route like this:

Route::get("certs","CertController@index")->name('certificate.front')->middleware('auth');

Now I wanted to add another middleware to this route, so I tried this:

Route::get("certs", "CertController@index")->name('certificate.front')->middleware('prevent-back-history','auth');

Now I don't get any error and it works But I wonder is this way better or not:

Route::get("certs", "CertController@index")->name('certificate.front')->middleware(['prevent-back-history','auth']);

So which is better and correct in this case?

Note that I don't want to use Route groups and needed to specify the middleware name directly to the route.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

在风中等你 2025-01-17 08:28:55

最后一个代码是正确的方法,因为如果您可以在代码中看到

Route::get("certs", "CertController@index")->name('certificate.front')->middleware(['prevent-back-history','auth']);

如果您想要超过特定的 1 个中间件,则应该使用数组来定义中间件,因此使用 [''] 是正确的方法,以防万一您想在路线中添加更多中间件。

The last code is the correct way, because if you can see in your code

Route::get("certs", "CertController@index")->name('certificate.front')->middleware(['prevent-back-history','auth']);

If you wanted to have more than specific 1 middleware, you should use an array to define the middleware, so using [''] is the correct way, in case you wanted to add more middleware into the route.

诠释孤独 2025-01-17 08:28:55

@apose7523
这两种方法都是正确的并且有效,因此选择哪种方法并不重要

Route::get('example',controller)->middleware('first', 'second');
Route::get('example',controller)->middleware(['first', 'second']);

@apose7523
Both the approaches are correct and are working, so it does not matter what approach you choose

Route::get('example',controller)->middleware('first', 'second');
Route::get('example',controller)->middleware(['first', 'second']);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文