Middleware - Acces granted - Solved

Hi all,

I just follow the cursus on how to implement Slim, and it’s working nicely !
But I get in trouble with a Middleware.

In fact, I’ve 3 kinds of routes :
• Frontend
• Backend
• Api

For the backend I need a credential to access the data.
Actually, I’m looking if the $_SESSION user is not null and if the route is not in an array (some pages from the front…).
But I don’t like this kind of process… If I add a new page, I’ve to exclude it… it’s not nice.
I would like to know if it’s possible to attach a Middleware to a “group” ?
Without the container, I can achieve this… but with the container… it’s broken.

Thanks for your advice !

Hi @stef157

Yes, you can also add a middleware to a routing group as described here: Route groups

$app->group('', function (RouteCollectorProxy $group) {
    // ...
})->add(new GroupMiddleware());

With a DI container it works even better, because you can lazy load middleware only when needed.

$app->group('', function (RouteCollectorProxy $group) {
    // ...
})->add(GroupMiddleware::class);

Ho… i was tired… it’s what I’ve done but didn’t write the “use” method…

Thanks for this !
We can close the thread :wink: