Aide pour comprendre ce code

Par Mikachu, il y a 11 ans


Salut,
J'ai besoin de l'aide d'un expert en Symfony pour répondre à une question:

Sur le symfony-demo, dont voici le lien github: Lien Symfony-demo

Dans la méthode newAction, et editAction du controller Admin/BlogController, il y a un return 'post' => $post.

Je me pose donc la question: A quoi cela sert? car dans les vues en question, on ne s'en sert pas (a part si j'ai fais l'impasse sur ca...)

Si quelqu'un pouvait m'aider à comprendre, cela serait cool.

Merci

Extrait code Admin/BlogController:

/** * Creates a new Post entity. * * @Route("/new", name="admin_post_new") * @Method({"GET", "POST"}) * * NOTE: the Method annotation is optional, but it's a recommended practice * to constraint the HTTP methods each controller responds to (by default * it responds to all methods). */ public function newAction(Request $request) { $post = new Post(); $post->setAuthorEmail($this->getUser()->getEmail()); $form = $this->createForm(new PostType(), $post); $form->handleRequest($request); // the isSubmitted() method is completely optional because the other // isValid() method already checks whether the form is submitted. // However, we explicitly add it to improve code readability. // See http://symfony.com/doc/current/best_practices/forms.html#handling-form-submits if ($form->isSubmitted() && $form->isValid()) { $post->setSlug($this->get('slugger')->slugify($post->getTitle())); $em = $this->getDoctrine()->getManager(); $em->persist($post); $em->flush(); return $this->redirectToRoute('admin_post_index'); } return $this->render('admin/blog/new.html.twig', array( 'post' => $post, 'form' => $form->createView(), )); } /** * Finds and displays a Post entity. * * @Route("/{id}", requirements={"id" = "\d+"}, name="admin_post_show") * @Method("GET") * @Security("post.isAuthor(user)") * * NOTE: You can also centralize security logic by using a "voter" * See http://symfony.com/doc/current/cookbook/security/voters_data_permission.html */ public function showAction(Post $post) { $deleteForm = $this->createDeleteForm($post); return $this->render('admin/blog/show.html.twig', array( 'post' => $post, 'delete_form' => $deleteForm->createView(), )); }

4 réponses

Vallyan, il y a 11 ans

Je serai aussi d'accord, apriori ... cela dit c'est pas bien compliqué a vérifier ;)

Pachenko, il y a 11 ans

Ca ne va certainement pas t'aider, mais le "$post" correspond à l'entity Post.

Au niveau de la vue, situé dans : "symfony-demo/app/Resources/views/admin/blog/new.html.twig"

La variable n'est pas utilisée. Donc ça n'a pas grand intérêt, à moins que je sois passé à côté de quelque chose.

Mikachu, il y a 11 ans

Salut,
Je pensais exactement pareil que toi. Pour ca que je demande au cas ou moi aussi je passe à coté de quelques chose.
Donc je rejoins ton "hypothèse" que les méthodes retourne post sans que cela ne soit utilisé, donc inutile... non ?

Pachenko, il y a 11 ans

En tout cas pas utile pour l'ajout.