Symfony2 Routing: Method Not Allowed (Allow: {Method})

旧时模样 提交于 2019-12-05 06:53:35
chuebert

Did you generate actions using CRUD?

I found an solution to address this problem.

/**
 * Deletes a Preisliste entity.
 *
 */
public function deleteAction(Request $request, $id)
{
    /*$form = $this->createDeleteForm($id);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $entity = $em->getRepository('MandantBundle:Preisliste')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Preisliste entity.');
        }

        $em->remove($entity);
        $em->flush();
    }

    return $this->redirect($this->generateUrl('preisliste'));*/

    $em = $this->getDoctrine()->getManager();
    $entity = $em->getRepository('MandantBundle:Preisliste')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Preisliste entity.');
    }

    $em->remove($entity);
    $em->flush();


    return $this->redirect($this->generateUrl('preisliste'));
}

The commented code is from CRUD and doesn't work. I get the same error (No route found for “GET ... ) I don't know why Symfony tries to use a form to delete. Only removes entity is the correct way for me.

Instead of this in your view :

<a href="{{ path('settings_delete', { 'id': settings.id }) }}">
    Delete
</a>

use a form :

{{ form_start(delete_form) }}
    <button type="submit">Delete</button>
{{ form_end(delete_form) }}

same for edit form

You only allow POST, PUT and DELETE methods, but you are accessing those routes via GET method.

so define your routes like this:

settings.editDefaults:
    path:      settings/{id}/defaults/edit/{widgetType}
    defaults:  { _controller: AppBundle:Settings:editDefaults }

settings.deleteDefaults:
    path:      settings/{id}/defaults/delete/{widgetType}
    defaults:  { _controller: AppBundle:Settings:deleteDefaults }

Or leave the DELETE, PUT and POST methods in, if you really need those restrictions and add GET method.

When you are accessing a URL with your browser, you are usually sending a your request via GET method. You can read more about these: Here And here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!