Symfony2 Routing: Method Not Allowed (Allow: {Method})
Asked Answered
M

4

8

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

Mesa answered 20/6, 2015 at 17:26 Comment(0)
I
4

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.

Ianteen answered 17/12, 2015 at 10:26 Comment(0)
P
3

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

Pimiento answered 10/6, 2017 at 16:16 Comment(0)
R
2

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

Renz answered 20/6, 2015 at 18:6 Comment(2)
Hey JaVe, thanks for the answer. Now that I have removed the methods, when deleting, it reports an error in the editAction, any reason why that route action is also called?Mesa
@AnthBieb: you removed methods part but I suggest you to keep it; this is semantically correct. error isn't in routes but into url request (you need to call it with DELETE, PUT or POST method setted)Wabble
G
1

For anyone else in coming across this issue not solved by the other answers check to see if you've accidentally named another method the same thing. If that happens one of them is ignored.

One way to sanity check this is by doing

php bin/console debug:router

And validating your routes make sense.

Grieg answered 6/11, 2020 at 2:45 Comment(1)
Thank you, I copied a method from one controller to another and forgot to change its name. This helped me fix it!Sorci

© 2022 - 2024 — McMap. All rights reserved.