We use a microfluidic ecology which generates non-uniform phage concentration gradients and micro-ecological niches to reveal the importance of time, spatial population structure and collective population dynamics in the {\em de novo} evolution of T4r bacteriophage resistant motile {\em E. coli}. An insensitive bacterial population against T4r phage occurs within 20 hours in small interconnected population niches created by a gradient of phage virions, driven by evolution in transient biofilm patches. Sequencing of the resistant bacteria reveals mutations at the receptor site of bacteriophage T4r as expected but also in genes associated with biofilm formation and surface adhesion, supporting the hypothesis that evolution within transient biofilms drives {\em de novo} phage resistance.