Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
|
symfony_4:fonctionnement:les_controleurs_preparation_des_donnees [2018/10/19 07:49] julien.guerin [Retours] |
symfony_4:fonctionnement:les_controleurs_preparation_des_donnees [2019/12/08 07:41] (Version actuelle) |
||
|---|---|---|---|
| Ligne 26: | Ligne 26: | ||
| * index (listing, page d' | * index (listing, page d' | ||
| * add | * add | ||
| - | * edit | + | * update |
| * delete | * delete | ||
| Ligne 117: | Ligne 117: | ||
| public function edit(Request $request, SWFile $SWFile){ ... } | public function edit(Request $request, SWFile $SWFile){ ... } | ||
| </ | </ | ||
| + | |||
| ---- | ---- | ||
| + | |||
| ==== Retours ==== | ==== Retours ==== | ||
| Ligne 126: | Ligne 128: | ||
| * Réponse en JSON (pour l' | * Réponse en JSON (pour l' | ||
| * Re-direction vers une route spécifique | * Re-direction vers une route spécifique | ||
| - | * Un code HTML ( 200, 404, 500 ) | + | |
| + | ---- | ||
| === Affichage d'une vue (TWIG) === | === Affichage d'une vue (TWIG) === | ||
| Ligne 152: | Ligne 155: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | ---- | ||
| === Envoi en AJAX === | === Envoi en AJAX === | ||
| + | |||
| + | Pour retourner un JSON, on créer un tableau et on retourne un objet JsonResponse. | ||
| + | |||
| <code PHP> | <code PHP> | ||
| + | use Symfony\Component\HttpFoundation\JsonResponse; | ||
| + | use Symfony\Component\HttpFoundation\Response; | ||
| + | |||
| + | ... | ||
| + | | ||
| + | $modalBarge = $this-> | ||
| + | ' | ||
| + | ' | ||
| + | )); | ||
| + | | ||
| $json_array = array( | $json_array = array( | ||
| - | ' | + | ' |
| + | ' | ||
| ); | ); | ||
| Ligne 162: | Ligne 181: | ||
| return $response-> | return $response-> | ||
| </ | </ | ||
| + | |||
| + | ---- | ||
| === Redirection vers une route === | === Redirection vers une route === | ||
| - | === Code HTML === | + | Pour rediriger vers une méthode précise, on redirige vers la route associée à celle-ci via la méthode : |
| + | <code PHP> | ||
| + | $this-> | ||
| + | </ | ||
| - | |||
| - | ---- | ||
| - | ===== Exemple ===== | ||
| <code PHP> | <code PHP> | ||
| - | <?php | + | /** |
| - | + | ||
| - | namespace App\Controller; | + | |
| - | + | ||
| - | use App\Entity\SWFile; | + | |
| - | use App\Form\SWFileType; | + | |
| - | use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; | + | |
| - | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | + | |
| - | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | + | |
| - | use Symfony\Component\HttpFoundation\Response; | + | |
| - | use Symfony\Component\HttpFoundation\Request; | + | |
| - | + | ||
| - | class SWFileController extends Controller | + | |
| - | { | + | |
| - | /** | + | |
| - | * Display SWFile list in tab | + | |
| - | * | + | |
| - | * @Route("/ | + | |
| - | * | + | |
| - | * @IsGranted(" | + | |
| - | * | + | |
| - | * @param Request $request | + | |
| - | * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response | + | |
| - | */ | + | |
| - | public function index(Request $request) | + | |
| - | { | + | |
| - | $manager = $this-> | + | |
| - | + | ||
| - | $tabSWFile = $manager-> | + | |
| - | + | ||
| - | return $this-> | + | |
| - | ' | + | |
| - | )); | + | |
| - | } | + | |
| - | + | ||
| - | | + | |
| * Add a SWFile | * Add a SWFile | ||
| * | * | ||
| * @Route("/ | * @Route("/ | ||
| * | * | ||
| - | * @IsGranted(" | + | * @IsGranted(" |
| * | * | ||
| * @param Request $request | * @param Request $request | ||
| Ligne 224: | Ligne 210: | ||
| if ($form-> | if ($form-> | ||
| - | | + | |
| - | | + | ... |
| - | $SWFile = $form-> | + | |
| - | + | //redirection vers la liste des swfiles après l' | |
| - | //MAJ des champs non disponibles sur le formulaire | + | |
| - | $SWFile-> | + | |
| - | $SWFile-> | + | |
| - | + | ||
| - | // | + | |
| - | $manager = $this-> | + | |
| - | $manager-> | + | |
| - | $manager-> | + | |
| - | + | ||
| - | return $this-> | + | |
| - | } | + | |
| - | + | ||
| - | return $this-> | + | |
| - | | + | |
| - | )); | + | |
| - | } | + | |
| - | + | ||
| - | /** | + | |
| - | * Edit a SWFile | + | |
| - | * | + | |
| - | * @Route("/ | + | |
| - | * | + | |
| - | * @IsGranted(" | + | |
| - | * | + | |
| - | * @param Request $request | + | |
| - | * @param SWFile $SWFile | + | |
| - | * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response | + | |
| - | */ | + | |
| - | public function edit(Request $request, SWFile $SWFile) | + | |
| - | { | + | |
| - | $form = $this-> | + | |
| - | + | ||
| - | $form-> | + | |
| - | + | ||
| - | if ($form-> | + | |
| - | // $form-> | + | |
| - | // but, the original `$task` variable has also been updated | + | |
| - | $SWFile = $form-> | + | |
| - | + | ||
| - | //MAJ des champs non disponibles sur le formulaire | + | |
| - | $SWFile-> | + | |
| - | $SWFile-> | + | |
| - | + | ||
| - | // | + | |
| - | $manager = $this-> | + | |
| - | $manager-> | + | |
| - | $manager-> | + | |
| return $this-> | return $this-> | ||
| } | } | ||
| - | + | | |
| - | | + | ... |
| - | ' | + | |
| - | | + | |
| } | } | ||
| + | </ | ||
| - | /** | + | ---- |
| - | * Delete a SWFile (not the file, just the database slot | + | |
| - | * | + | |
| - | * @Route("/ | + | |
| - | * | + | |
| - | * @IsGranted(" | + | |
| - | * | + | |
| - | * @param Request $request | + | |
| - | * @param SWFile $SWFile | + | |
| - | * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response | + | |
| - | */ | + | |
| - | public function delete(Request $request, SWFile $SWFile) | + | |
| - | { | + | |
| - | $manager = $this-> | + | |
| - | //retrait du SWFile | ||
| - | $manager-> | ||
| - | $manager-> | ||
| - | |||
| - | return $this-> | ||
| - | } | ||
| - | |||
| - | } | ||
| - | </ | ||