framework-web:symfony:td3

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
framework-web:symfony:td3 [2018/02/21 01:01] – [Composant HtmlButtonGroups] jcheronframework-web:symfony:td3 [2019/08/31 14:21] (Version actuelle) – modification externe 127.0.0.1
Ligne 32: Ligne 32:
 ===== Création du projet, intégration des composants ===== ===== Création du projet, intégration des composants =====
  
-Exécuter le script de création de la base de données :+Exécuter le script de création de la base de données : {{:framework-web:symfony:td3:projects.sql|}}
  
 <sxh bash:gutter:false> <sxh bash:gutter:false>
-mysql -u root < path/to/boards.sql+mysql -u root < path/to/projects.sql
 </sxh> </sxh>
  
Ligne 382: Ligne 382:
  
  
-Ajouter la route **tags** dans le contrôleur **ProjectsController** :+Créer le controller **Tags** et la route **tags** : 
 + 
 +<sxh php;title:src/Controller/TagsController.php> 
 +namespace App\Controller; 
 + 
 +use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
 +use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
 +use Symfony\Component\HttpFoundation\Response; 
 +use App\Repository\TagRepository; 
 +use App\Services\semantic\TagsGui; 
 + 
 +class TagsController extends Controller{
  
-<sxh php;title:src/Controller/ProjectsController.php> 
-... 
     /**     /**
      * @Route("/tags", name="tags")      * @Route("/tags", name="tags")
      */      */
-    public function tags(TagsGui $gui,TagRepository $tagRepo){+    public function index(TagsGui $gui,TagRepository $tagRepo){
     $tags=$tagRepo->findAll();     $tags=$tagRepo->findAll();
     $dt=$gui->dataTable($tags);     $dt=$gui->dataTable($tags);
     return new Response($dt);     return new Response($dt);
     }     }
-    +
 </sxh> </sxh>
  
Ligne 420: Ligne 429:
 } }
 </sxh> </sxh>
 +
 +{{:framework-web:symfony:td3:tags-2.png?|Tags}}
 +
 +==== Formulaire de modification ====
 +
 +Ajouter une méthode **frm** dans **TagsGui**
 +<sxh php;title:src/Services/semantic/TagsGui.php>
 + public function frm(Tag $tag){
 + $frm=$this->_semantic->dataForm("frm-tag", $tag);
 + return $frm;
 + }
 +</sxh>
 +
 +Créer la vue associée :
 +
 +<sxh html;title:templates/Tags/frm.html.twig>
 +{{ q["frm-tag"] | raw }}
 +{{ script_foot | raw }}
 +</sxh>
 +
 +Créer la route affichant le formulaire dans le contrôleur **Tags** :
 +
 +<sxh php;title:src/Controller/TagsController.php>
 +...
 +    /**
 +     * @Route("tag/update/{id}", name="tag_update")
 +     */
 +    public function update(Tag $tag,TagsGui $tagsGui){
 +    $tagsGui->frm($tag);
 +    return $tagsGui->renderView('Tags/frm.html.twig');
 +    }
 +</sxh>
 +
 +
 +Ajouter le bouton d'édition dans le composant dataTable des tags :
 +
 +<sxh php;title:src/Services/semantic/TagsGui.php;highlight:[12,13,14]>
 +...
 +class TagsGui extends JquerySemantic{
 + public function dataTable($tags){
 + $dt=$this->_semantic->dataTable("dtTags", "App\Entity\Tag", $tags);
 + $dt->setFields(["tag"]);
 + $dt->setCaptions(["Tag"]);
 + $dt->setValueFunction("tag", function($v,$tag){
 + $lbl=new HtmlLabel("",$tag->getTitle());
 + $lbl->setColor($tag->getColor());
 + return $lbl;
 + });
 + $dt->addEditButton();
 + $dt->setUrls(["edit"=>"tag/update"]);
 + $dt->setTargetSelector("#update-tag");
 + return $dt;
 + }
 +</sxh>
 +
 +Modifier la route **/tags** pour qu'elle sollicite une vue et puisse intégrer plus facilement des scripts côté client :
 +
 +<sxh php;title:src/Controller/TagsController.php>
 +...
 +    /**
 +     * @Route("/tags", name="tags")
 +     */
 +    public function tags(TagsGui $gui,TagRepository $tagRepo){
 +    $tags=$tagRepo->findAll();
 +    $gui->dataTable($tags);
 +    return $gui->renderView('Tags/index.html.twig');;
 +    }
 +    
 +</sxh>
 +
 +Ajouter la vue **templates/Tags/index.html.twig** : cette vue contient une zone HTML d'id **update-tag** dans laquelle sera affiché le formulaire de modification
 +
 +<sxh html;title:templates/Tags/index.html.twig>
 +<div id="update-tag"></div>
 +{{ q["dtTags"] | raw }}
 +{{ script_foot | raw }}
 +</sxh>
 +
 +
 +Implémenter la méthode **update** dans **TagRepository** :
 +<sxh php;title:src/Repository/TagRepository.php;highlight:[9,10,11,12]>
 +...
 +class TagRepository extends ServiceEntityRepository
 +{
 +    public function __construct(RegistryInterface $registry)
 +    {
 +        parent::__construct($registry, Tag::class);
 +    }
 +
 +    public function update(Tag $tag){
 +    $this->_em->persist($tag);
 +    $this->_em->flush();
 +    }
 +</sxh>
 +
 +Créer la route **tag/update** dans **TagsController** :
 +<sxh php;title:src/Controller/TagsController.php>
 +...
 +    /**
 +     * @Route("tag/submit", name="tag_submit")
 +     */
 +    public function submit(Request $request,TagRepository $tagRepo){
 +    $tag=$tagRepo->find($request->get("id"));
 +    if(isset($tag)){
 +    $tag->setTitle($request->get("title"));
 +    $tag->setColor($request->get("color"));
 +    $tagRepo->update($tag);
 +    }
 +    return $this->forward("App\Controller\TagsController::tags");
 +    }
 +</sxh>
 +
 +Améliorer le formulaire de modification des tags :
 +
 +<sxh php;title:src/Services/semantic/TagsGui.php>
 +...
 + public function frm(Tag $tag){
 + $colors=Color::getConstants();
 + $frm=$this->_semantic->dataForm("frm-tag", $tag);
 + $frm->setFields(["id","title","color","submit","cancel"]);
 + $frm->setCaptions(["","Title","Color","Valider","Annuler"]);
 + $frm->fieldAsHidden("id");
 + $frm->fieldAsInput("title",["rules"=>["empty","maxLength[30]"]]);
 + $frm->fieldAsDropDown("color",\array_combine($colors,$colors));
 + $frm->setValidationParams(["on"=>"blur","inline"=>true]);
 + $frm->onSuccess("$('#frm-tag').hide();");
 + $frm->fieldAsSubmit("submit","positive","tag/submit", "#dtTags",["ajax"=>["attr"=>"","jqueryDone"=>"replaceWith"]]);
 + $frm->fieldAsLink("cancel",["class"=>"ui button cancel"]);
 + $this->click(".cancel","$('#frm-tag').hide();");
 + $frm->addSeparatorAfter("color");
 + return $frm;
 + }
 +</sxh>
 +
 +{{:framework-web:symfony:td3:tags-frm-2.png?|Tags form}}
  
  • framework-web/symfony/td3.1519171295.txt.gz
  • Dernière modification : il y a 6 ans
  • (modification externe)