Phalanxes aren't the best units for street fights, because of the problem you have mentioned: it is difficult to protect their flanks with mobile units.