1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
class EntreeController extends AbstractController
{
/**
* @Route("/carte_restaurant/{id<\d+>}", name="carte_restaurant")
*/
public function carte(Request $request)
{
# création d'une entrée
$entree = new Entree();
# création du formulaire EntreeFormType
$form_entree = $this->createForm(EntreeFormType::class, $entree)
->handleRequest($request);
# Soumission du Formulaire
if ($form_entree->isSubmitted() && $form_entree->isValid()) {
$photo_entree = $entree->getPhoto();
if(null !== $photo_entree) {
# 1. Traitement de l'upload de l'image
// $photo stores the uploaded file
/** @var UploadedFile $photo_entree */
$photo_entree = $entree->getPhoto();
$fileName_entree = $this->slugify($entree->getNom()) . '.' . $photo_entree->guessExtension();
// Move the file to the directory where images are stored
try {
$photo_entree->move(
$this->getParameter('menus_assets_dir'),
$fileName_entree
);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
# Mise à jour de l'image
$entree->setPhoto($fileName_entree);
}
# Sauvegarde en BDD
$em = $this->getDoctrine()->getManager();
$em->persist($entree);
$em->flush();
# Notification
$this->addFlash('notice_entrée',
'Félicitations, votre entrée a bien été ajoutée!');
}
return $this->render('restaurant/carte.html.twig', [
'form_entree' => $form_entree->createView(),
]);
}
} |
Partager