Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

so in routing.yml I have the following routes defined in order to edit and delete specific settings:

routing.yml:

settings.editDefaults:
    path:      settings/{id}/defaults/edit/{widgetType}
    defaults:  { _controller: AppBundle:Settings:editDefaults }
    methods:  [POST, PUT]

settings.deleteDefaults:
    path:      settings/{id}/defaults/delete/{widgetType}
    defaults:  { _controller: AppBundle:Settings:deleteDefaults }
    methods: [DELETE]

And in my controller I have the correct actions defined: SettingController.php:

/**
 * edit the default settings of a hotel/widget
 */
public function editDefaultsAction(Request $request)
{ 
   //Edit logic
}

/**
 * delete a default setting of a hotel/widget
 */
public function deleteDefaultsAction($hotelId, $widgetType)
{
  //Delete logic
}

In the second action I only need the id and widgetType passed so I can query for and remove the selected record.

When I go to either of the routes I get the following:

Edit Route Error:

No route found for "GET /settings/2b2acd55-0dd6-11e5-8107-621ae3320fd4/defaults/edit/default": Method Not Allowed (Allow: POST, PUT)

Delete Route Error:

No route found for "GET /settings/2b2acd55-0dd6-11e5-8107-621ae3320fd4/defaults/delete/default": Method Not Allowed (Allow: DELETE)

But when I remove one and leave the other they work fine. I'm assuming it's the path definitions that are similar? Is it possible for me to keep the same paths and not get this error? What am I not understanding?

Thanks for your help, Anth

like image 697
Anth Bieb Avatar asked Jun 20 '15 17:06

Anth Bieb


2 Answers

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.

like image 100
chuebert Avatar answered Sep 19 '22 15:09

chuebert


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

like image 25
Théo Attali Avatar answered Sep 22 '22 15:09

Théo Attali