Yes, I see that now. What about adding two middlewares to the route, the first one you add would check for the public
attribute and if so pass along to the $next
middleware. If there is no public
attribute then it would validate the token.
<?php
require __DIR__ . '/../vendor/autoload.php';
$app = new \Slim\App;
$app->add(function ($req, $res, $next) {
$res->getBody()->write("second\n");
$res = $next($req, $res);
$res->getBody()->write("eighth\n");
return $res;
});
$app->get('/', function ($req, $res) {
$res->getBody()->write("fifth->app/body\n");
return $res;
})->add(function ($req, $res, $next) {
$res->getBody()->write("fourth->checkAttributeHere\n");
$res = $next($req, $res);
$res->getBody()->write("sixth\n");
return $res;
})->add(function ($req, $res, $next) {
$res->getBody()->write("third->addAttributeHere\n");
$res = $next($req, $res);
$res->getBody()->write("seventh\n");
return $res;
});
$app->add(function ($req, $res, $next) {
$res->getBody()->write("first\n");
$res = $next($req, $res);
$res->getBody()->write("ninth\n");
return $res;
});
$app->run();
outputs
first
second
third->addAttributeHere
fourth->checkAttributeHere
fifth->app/body
sixth
seventh
eighth
ninth
But I see where adding that (in the case above forth) middleware would be tricky if you want to add it to everything in a group at the end.
Maybe use determineRouteBeforeAppMiddleware and inspect the route to check and see if the route is public?