【问题标题】:Edit/delete not working symfony 3编辑/删除不工作的 symfony 3
【发布时间】:2017-08-25 18:17:18
【问题描述】:

我的编辑/删除工作正常,当我继续处理我的项目时,它在某处停止工作。当我尝试编辑或删除时收到此消息。

“未找到“DELETE /shrubs/1/edit”的路由:不允许的方法(允许:GET、POST、HEAD)”

我认为控制器处理了这个,还是我也需要在 routing.yml 中添加一些东西?

我的控制器中有这个:

/**
     * Displays a form to edit an existing Shrubs entity.
     *
     * @Route("/{id}/edit", name="shrubs_edit")
     * @Method({"GET", "POST"})
     */
    public function editAction(Request $request, Shrubs $shrub)
    {
    $deleteForm = $this->createDeleteForm($shrub);
    $editForm = $this->createForm('AppBundle\Form\ShrubsType', $shrub);
    $editForm->handleRequest($request);

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

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

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

/**
 * Deletes a Shrubs entity.
 *
 * @Route("/{id}", name="shrubs_delete")
 * @Method("DELETE")
 */
public function deleteAction(Request $request, Shrubs $shrub)
{
    $form = $this->createDeleteForm($shrub);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->remove($shrub);
        $em->flush();
    }

    return $this->redirectToRoute('shrubs_index');
}

/**
 * Creates a form to delete a Shrubs entity.
 *
 * @param Shrubs $shrub The Shrubs entity
 *
 * @return \Symfony\Component\Form\Form The form
 */
private function createDeleteForm(Shrubs $shrub)
{
    return $this->createFormBuilder()
        ->setAction($this->generateUrl('shrubs_delete', array('id' => $shrub->getId())))
        ->setMethod('DELETE')
        ->getForm()
    ;
}

我的编辑页面有这个:

 <button type="submit" id="register-submit-btn" class="btn btn-success">Edit</button>

    {{ form_start(delete_form) }}
    <input type="submit" value="Delete">
    {{ form_end(delete_form) }}

如果我的 routing.yml 需要一些东西,我会添加什么?

【问题讨论】:

    标签: symfony crud


    【解决方案1】:

    尝试在您的路由中添加DELETE 方法,这就是错误消息的含义。 您使用DELETE 方法,但您只允许GET, POST

    /**
     * Displays a form to edit an existing Shrubs entity.
     *
     * @Route("/{id}/edit", name="shrubs_edit")
     * @Method({"DELETE", "GET", "POST"})
    */
    public function editAction(Request $request, Shrubs $shrub)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      相关资源
      最近更新 更多