在路由组中分配中间件与在控制程序的构造函数中启动中间件之间有什么区别? [英] Is there any difference between assigning middleware in a route group and launching it in controler's constructor?

查看:83
本文介绍了在路由组中分配中间件与在控制程序的构造函数中启动中间件之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在这样的路由中,中间的中间件之间是否有任何区别:

I am wondering if there is any adifference between asiigning middleware in a route like this :

Route::patch('/edit/{column}/{id}',['middleware' => 'auth', 'uses' => 'ResourceController@editCompany']); 

并在控制器的构造函数中启动

and launching it in controller's constructor

public function __construct()
{
  $this->middleware('auth');
}

是否一样?它还会执行其他任何操作,然后检查我是否已登录吗?

Is it the same? Does it do anything else then checking if I am logged in?

推荐答案

完全相同.问题是,当您在构造函数中添加它时,您需要记住将其添加到您要受身份验证保护的每个新控制器中.

It is completely the same. The problem is when you add that in the constructor you need to remember to add it in every new controller that you want to be auth protected.

在路由文件中,您可以对多个端点进行分组并在所有端点中应用中间件:

While in the routes file, you can group multiple endpoints and apply the middleware in all of them:

Route::group(['middleware' => 'auth'], function() {
 // all routes here that need to be auth protected.
});

这篇关于在路由组中分配中间件与在控制程序的构造函数中启动中间件之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆