Dmitry Divin Dmitry Divin - 4 months ago 20
PHP Question

Symfony CRUD controller - how does it works?

I'm trying to understand how is Symfoy CRUD controllers works, i've googled a lot and can't find any answers.

So the question is, how does controller knows, which entity is passed to route?
For example:

In this index route, we are calling doctrine manager and then pulling all the comments from database.

/**
* Lists all Comment entities.
*
* @Route("/", name="admin_comment_index")
* @Method("GET")
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();

$comments = $em->getRepository('AppBundle:Comment')->findAll();

return $this->render('comment/index.html.twig', array(
'comments' => $comments,
));
}


but at next "new" action we are not calling any doctrine instances.Controller seems alredy knows which entity is operating.

/**
* Creates a new Comment entity.
*
* @Route("/new", name="admin_comment_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
$comment = new Comment();
$form = $this->createForm('AppBundle\Form\CommentType', $comment);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($comment);
$em->flush();

return $this->redirectToRoute('admin_comment_show', array('id' => $comment->getId()));
}

return $this->render('comment/new.html.twig', array(
'comment' => $comment,
'form' => $form->createView(),
));
}


I guess it's because second route gets "Request" object, is entity stored in it? I'd like to have some deeper explanation.

UPDATE: "new" action seems clear to me now,it was a bad example of what i'm trying to figure, but let's see the "edit" action:

public function editAction(Request $request, Comment $comment)
{
$deleteForm = $this->createDeleteForm($comment);
$editForm = $this->createForm('AppBundle\Form\CommentType', $comment);
$editForm->handleRequest($request);

if ($editForm->isSubmitted() && $editForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($comment);
$em->flush();

return $this->redirectToRoute('admin_comment_edit', array('id' => $comment->getId()));
}

return $this->render('comment/edit.html.twig', array(
'comment' => $comment,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}


This time, form is already rendered with data in it, but we are only passing 'id" in request

<a href="{{ path('admin_comment_edit', { 'id': comment.id }) }}">edit</a>


From where data comes this time? Seems like from comment object,which is passed into controller, but i dont see where it come from.
Sorry for my noobish questions and bad english!

Answer

I think you are making it complicated assuming controller knows what to do.

It's actually what code is written inside a controller defines what controller does. For example :

indexAction supposed to get you list of comments from database, hence you need to get EntityManager first to fetch the data. this controller doesn't deal with form as its not required.

newAction supposed to create a new instance of the Comment entity and generate a form to be filled up by user, when submitted Request parameter catches all data and saves to the database. hence, unless you get data from a submitted form, you don't need entity manager to deal with database.

Also, don't assume these are limited to what controller can do, You can customise any controller as per you requirement.

Hope it make sense.

Comments