For the red sauce: Heat the olive oil in a large saucepan. Add the garlic and onions and saute until tender, 5 minutes. Add the tomatoes, red pepper flakes and some salt and black pepper and simmer for 20 minutes. Tear the basil leaves and stir into the sauce at the end of simmering.
For the aubergine parmigiana: Preheat the oven to 200°C. Pour 2 inches of olive oil into a heavy-bottomed pot or high-sided frying pan and heat to 180°C.
Pour the flour into a wide casserole dish. Whisk in salt and black pepper. Dredge the aubergine slices in the flour, and then fry them in the hot oil in batches. When they are browned, remove them from the oil and lay them on a paper-towel-lined baking sheet. Repeat as necessary. Let cool.
Start layering your ingredients in individual gratin dishes or in 2 disposable trays if making family-style servings: a few slices of fried aubergine, a small spoonful of red sauce, a couple of handfuls of mozzarella, a couple of tablespoons of Parmesan, a few leaves of basil. Repeat the layering 2 to 3 times (3 is better if your dish can accommodate it). For the top layer, finish with a final spoonful of red sauce, mozzarella and Parmesan. Bake until bubbly and golden on top, 25 to 30 minutes. Reserve any remaining red sauce for another use.
Serve garnished with a few leaves of basil, a sprinkle of Parmesan, and a touch of extra-virgin olive oil.