Asfi, Romans and Goths and finally by Muslims in the 11th century. It was a ribat (a type of fortified monastery) in the 13th century and was mentioned by historian Ibn Khaldun. The Portuguese occupied Safi (1508-1541) and built a citadel which now surrounds the 18th century keshla (military enclave). Safi flourished in the late 16th and early 17th centuries. Stretching along the coast and across the interior plateau, the city is a mix of medieval and modern architecture. The port of Safi serves Marrakech some 140 km to the southeast and exports phosphates from Youssoufia, to which it is connected by rail. It is an important fishing port with sardine canneries and also produces textiles.