How-to pass a variable to middleware per route

I’m working on a middleware to authorize route
I’m using

'determineRouteBeforeAppMiddleware'=>true,

How can I pass a variable to the middleware based on the route like for example I can set the route name or group ?

Here you can find some example:

http://www.slimframework.com/docs/v3/cookbook/retrieving-current-route.html

Thanks for point out it but as I have written I need to add an extra value when registering a route cause I’m already using name and grop.
I need to do something like

$dummy=“anything”;
$app->get(’/test’, function (Request $request, Response $response, array $args) use ($container) {
//empty
})->setCutom($dummy);

the value of $dummy will be retrieved in the middleware just like name and group

Thanks again for your answer

what exactly you are trying achieve ? this is smelly

I have my routes in a db I already register routes like

$app->get($routeRecord->pattern, function (Request $request, Response $response, array $args) use ($container, $routeRecord, $me) {
})->setName($routeRecord->name);
where routeRecord is a row stdClass from db

the “real” dispatching of the route is done by a middleware that map uri to controller and method

in the middleware I know I can get route details this way

//retrieving route
$route = $request->getAttribute(‘route’);
// return NotFound for non existent route
if (empty($route)) {
throw new NotFoundException($request, $response);
}

    $name = $route->getName();
    $groups = $route->getGroups();

*** here I need to retrieve also the whole routeRecord

ok, this is HOW
but WHAT you are trying achieve by this?

why is not route->getName enough for you ? (you can retrieve that one specific record from DB based on that btw)

First reason
I already have a full list of records
I don’t want to execute another query for each route

Second
by design I try to reduce the dependency between objects
running the query inside middleware involves passing in or create connection
middleware must also know the table …

I knows there are other methods for example globalize the route list use it in the middleware and then destroy it
Or to add the route list to the container

but the question I’d like to find answer if there is a way to pass in to the middleware “some thing”

I have a solution : writing the middleware as closure
$routeList=[‘dummy’=>‘fake’];
$app->add(function ($request, $response, $next)use($routeList) {
die(var_dump($routeList));
$response->getBody()->write(‘BEFORE’);
$response = $next($request, $response);
$response->getBody()->write(‘AFTER’);

return $response;

});

$app->get(’/’, function ($request, $response, $args) {
$response->getBody()->write(’ Hello ');

return $response;

});

though I don’t like closure :frowning:

so what you are doing; is you are loading large number of records
each record has a “url” and “controller” context (string I guess?) which should be invoked

why you don’t have just one general route (not middleware) and why you will not make just one query for one row
your solution is much more resource expensive, sorry byt loading “all records” for each request to build router is just…naive (to avoid using rude words)

you can do something like

// your routes builder

$app->get('/{recordUriPart:[a-zA-Z0-9\_\-]+}', YourController::class . ':yourMethod'); // modify regexp by your needs

and than

class YourController {
public function __construct(Container $container) {...setup DB access here}

public function yourMethod($request, $response, $args) {
$yourRecord  = $this->db->select('some nice select by you with . ' $args['recordUriPart']);
// do whatever you want here

return $response;
}

}