编辑/删除不工作的symfony 3

时间:2017-03-31 12:57:16

标签: symfony crud

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

"找不到" 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我会添加什么?

1 个答案:

答案 0 :(得分:0)

尝试在路由中添加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)