Routes group and many function in middleware

Slim 3

How to call function midleware in Routes group?
I need to call one function by route within group but after execute return error.

it’s is possible?

e.g

$app->group(’/post’,function () use($app){
$app->post(’/insertUsuario’,function($request, $response){
$this->logger->info(“BEGIN - Rota - insertUsuario”);
$usuario = $request->getParsedBody();
return appController::insertUsuario($usuario)
$this->logger->info(“END - Rota - insertUsuario”);
}->add($ValidMail);
$app->post(’/login’, function($request, $response){
$this->logger->info(“BEGIN - Rota - login”);
$usuario = $request->getParsedBody();
return appController::login($usuario);
}->add($Aut));
});

in my middleware…

$Aut = function ($request, $response, $next) {
/$response->getBody()->write(‘BEFORE’);/
$headers = apache_request_headers();
$this->logger->info(‘Validando autenticação do usuário = [’.$headers[‘usuario’].’]’);
if ((isset($headers[‘autorizacao’])) && (isset($headers[‘usuario’]))) {
$db = new DbLib();
$api_key = $headers[‘autorizacao’];
$usuario = $headers[‘usuario’];
$return = ($db->validaApiKey($api_key,$usuario));
if ($return == SUCESSO){
$response = $next($request, $response);
} else if ($return == CHAVES_DE_ACESSO_INVALIDAS) {
$response = $response->WithStatus(401)->write(“Chaves de acesso invalidas”);
} else if ($return == CHAVE_DE_ACESSO_VAZIA) {
$response = $response->WithStatus(401)->write(“Api key vazia”);
} else {
$response = $response->WithStatus(401)->write(“Api key invalida”);
}
}else{
$response = $response->WithStatus(401)->write(“Chaves de acesso vazias”);
}
/$response->getBody()->write(‘AFTER’);/
$this->logger->info($response);
return $response;
};

$ValidMail = function ($request, $response, $next){
/$response->getBody()->write(‘BEFORE’);/
$users = $request->getParsedBody();
$email = $users[‘email’];
$pass = $users[‘senha’];
$this->logger->info(‘Validando e-mail para cadastro = [’.$email.’]’);
$er = "/^(([0-9a-zA-Z]+[-._+&])[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}){0,1}$/";
//validando o e-mail
if (!preg_match($er, $email)){
$response = $response->WithStatus(401)->write(“E-mail invalido”);
$this->logger->error($response);
return $response;
}
//Validando a quantidade minima de 8 digitos para a senha
$pass = str_replace(" “,”",$pass);
if (strlen($pass) <> TAM_SENHA){
$response = $response->WithStatus(401)->write(“A senha deve conter 8 digitos”);
$this->logger->error($response);
return $response;
}
//passou pela validação
$response = $next($request,$response);
$this->logger->info($response);
/
$response->getBody()->write(‘AFTER’);*/
return $response;
};

Thanks,