Symfony CRUD 控制器 - 它是如何工作的?

Symfony CRUD controller - how does it works?

我想了解 Symfoy CRUD 控制器是如何工作的,我用谷歌搜索了很多但找不到任何答案。

那么问题来了,controller怎么知道,哪个entity被传递给了route? 例如:

在这个索引路由中,我们正在调用学说管理器,然后从数据库中提取所有评论。

 /**
 * 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,
    ));
}

但在接下来的 "new" 行动中,我们不会调用任何学说 instances.Controller 似乎已经知道哪个实体在运作。

/**
 * 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(),
    ));
}

我猜是因为第二条路由得到了"Request"对象,里面存的是entity吗?我想有更深入的解释。

更新:"new" 动作现在对我来说似乎很清楚,这是我想弄清楚的一个不好的例子,但让我们看看 "edit" 动作:

 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(),
    ));
}

这一次,表单已经渲染了其中的数据,但我们只在请求中传递了“id”

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

这次数据来自哪里?似乎来自评论对象,它被传递到控制器,但我看不到它来自哪里。 抱歉我的问题和糟糕的英语!

newAction 确实在 if 语句中获取了一个 EntityManager 实例。

if ($form->isSubmitted() && $form->isValid()) {
    $em = $this->getDoctrine()->getManager();
//...

然后它使用这个管理器来持久化对象并刷新。

当您加载 newAction 页面时,它会创建一个新的评论对象,并将其发送到表单生成器。之后,它确保将表单数据放置到新的 Comment 对象上,使其得以持久化。

在 newAction 函数中:

首先,您的表单与您的实体映射:

$comment = new Comment();
$form = $this->createForm('AppBundle\Form\CommentType', $comment);

所以 Symfony 知道您在处理表单中的 Comment 个对象

然后当您提交表单时,handleRequest() 识别到这一点并立即将提交的数据写回 Comment 对象

最后,如果对象有效,你只需要将它保存在数据库中,感谢 entityManager

所以在请求和你的表单类型之间 Symfony 知道他想要什么

假设控制器知道该怎么做,我认为你把它弄复杂了。

实际上,控制器内部编写的代码定义了控制器的功能。例如:

indexAction 应该从数据库中获取评论列表,因此您需要先获取 EntityManager 来获取数据。此控制器不处理表单,因为它不是必需的。

newAction 应该创建 Comment 实体的新实例并生成一个由用户填写的表单,提交时 Request 参数捕获所有数据并保存到数据库。因此,除非您从提交的表单中获取数据,否则您不需要实体管理器来处理数据库。

此外,不要假设这些仅限于控制器可以执行的操作,您可以根据需要自定义任何控制器。

希望它有意义。

当我通过 generate:doctrine:crud 创建第一个控制器时,我也在想同样的问题。

由于我也是 symfony 的新手,所以我所说的仍然基于一些假设。如果我错了,我感谢任何更正,因为它有助于我学习。

内核似乎可以根据类型提示识别您的控制器函数接受的内容。由此,它确定您的控制器功能是否接受 Request 对象。 此外,路由中的参数根据提示类型进行解析和解析。这应该 然后通过 Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter Class 完成魔术。 通过键入提示您想要的对象 Comment,id 参数将转换为关联的对象并从您的数据库中加载。

顺便说一句,到目前为止我还想不通:是否可以在我的路由中使用与在我的函数中不同的参数顺序?

例如有可能做 /user/{user_id}/{comment_id}

public function funWithUserComment( Request $request, Comment $comment, User $user)
{
   //..
}

希望对你有所帮助。