我在Laravel 5.2应用中使用 controller middlewares 。根据文档,要排除中间件处理的特定操作,我需要使用except
数组:
class UserController extends Controller
{
public function __construct() {
// Exclude foo- and barAction from auth middleware
$this->middleware('auth', ['except' => [
'fooAction',
'barAction',
]]);
}
}
当然,控制器中的方法总数几乎总是大于 routes.php 中链接到特定路径的方法数。因此,除了路线行动之外,还会有其他行为,严格地处理逻辑 - 公共或私人方法。
我是否需要从中间件中排除所有这些非路由操作或排除路由操作就足够了?
编辑:
我会说其他非路由方法 - 因为它们无法从外部访问 - 不需要从中间件中排除。问题是:每次访问中间件时都会为它们运行吗?我不会这么说但确实很好。
答案 0 :(得分:1)
您的 $ this-> middleware()
方法在 Illuminate \ Routing \ Controller
:它只是将其参数保存在只能通过getter访问的受保护属性中。
除了在测试中,getter仅在 调用两个地方


 Illuminate \ Foundation \ Console \ RouteListCommand
(处理 route:list
Artisan命令);和
Illuminate \ Routing \ ControllerDispatcher
(将路由调用分派给控制器)。