You should check the global middleware if it doesn't exist there.
Lumen is applying Middleware to all Routes
I thought I followed the documentation on this, but it doesn't seem to be working as I expect it to. Using Lumen 5.4, I've created a "basic authentication" Middleware that I applied to a single route. However, when I visit any route, the browser asks for authentication.
app.php
$app->routeMiddleware([
'auth.basic' => \App\Http\Middleware\BasicAuthMiddleware::class
]);
Added the middleware reference to app->routeMiddleware() which I thought means I had to apply it a route at a time.
web.php
$app->get('/Members/', function () {
return response()->json(Member::all(), 200, [], JSON_PRETTY_PRINT);
});
$app->post('/getData/', function (Request $r) use ($app) {
// Way too much logic for a closure, but that isn't the point of this post
return response()->json($responseArray, 200, [], JSON_PRETTY_PRINT);
})->middleware('\App\Http\Middleware\BasicAuthMiddleware::class');
In the two examples from my web.php file above, one (GET /Members) should have no Middleware applied and one (POST /getData) should.
However, when I browse to mysite/Members, the browser asks for authentication.
What did I do wrong?
All right, so it looks like $app->post() winds up calling RoutesRequests::post(), which returns an object that implements ReoutesRequests. Therefore when I do $app->post()->middleware(), it is effectively calling RoutesRequests::middleware(), which adds the Middleware to all the routes served by the same RoutesRequests object.
That is why using the ->middleware() approach associated the auth.basic middleware with all routes and why using the $app->post('', ['middleware' => 'auth.basic', function... approach associates the middleware with only the specific route.
Please or to participate in this conversation.