How to conditionally add middleware and names to routes?

I’m trying to define route info in an array and loop through it to register routes with slim. But, I am having difficulty figuring out how to conditionally add middleware and route names based on array parameters. This is probably more of a PHP question than a slim question, but any help is appreciated.

See this URL for someone doing a similar thing: http://help.slimframework.com/discussions/questions/812-configuring-routes-dynamically

I’m not sure that solution works for the latest slim, and it’s different in that he is using callback functions for middleware, while I’m using invokable classes. Plus his naming is not done conditionally.

So far, I have a fn like this which works if the route array has middleware and a name, but I’m stuck on the conditionals. (sorry if the code format is not clean)

function register(Slim\App $slim, \Slim\Container $container, array $routeInfo)

{
$method = $routeInfo[‘method’];
$path = $routeInfo[‘path’];
$handler = $routeInfo[‘handler’];
$name = $routeInfo[‘name’];
$middleware = $routeInfo[‘middleware’];

$slim->$method(
    $path,
    $handler
    )->add(new $middleware($container)
)->setName($name);

}

It seems easier than I thought.

$a = $slim->$method(
    $path,
    $handler
);

if (isset($name) && isset($adminMinimumPermissions[$name])) {
    $a->add(new AuthorizationMiddleware($container, $adminMinimumPermissions[$name]));
}