1

I understand when allowing similarly accessible routes, that the order of the routes matter.

Where I'm confused is why when submitting a DELETE request to this route, does it match to the GET route, instead of ignoring it and trying the matched method one below it?

    /**
     * @Route("/{game}")
     * @Method({"GET"})
     */
    public function single(Request $request, GameSerializer $gameSerializer, Game $game) {
        $out = $gameSerializer->bind($game);
        return new JsonResponse($out);
    }

    /**
     * @Route("/{game}")
     * @Method({"DELETE"})
     */
    public function remove(Request $request, Game $game) {
        $em = $this->getDoctrine()->getManager();


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

        return new JsonResponse([], 200);
    }

enter image description here

enter image description here

Full disclosure

I understand why it matches the top most route based on strictly patterns

I dont understand why the access method is getting ignored when doing so

So, just to test, I adjusted to move the DELETE based route up above the GET route


    /**
     * @Route("/{game}")
     * @Method({"DELETE"})
     */
    public function remove(Request $request, Game $game) {
        $em = $this->getDoctrine()->getManager();

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

        return new JsonResponse([], 200);
    }

    /**
     * @Route("/{game}")
     * @Method({"GET"})
     */
    public function single(Request $request, GameSerializer $gameSerializer, Game $game) {
        $out = $gameSerializer->bind($game);
        return new JsonResponse($out);
    }

only.. for this to happen when I tried getting an existing non-test record by performing a basic operation of visiting the url in a browser (so, GET)

enter image description here

and oh boy, did it ever delete that record.

Why is the Access Method being ignored?

RedactedProfile
  • 2,748
  • 6
  • 32
  • 51
  • Which version of Symfony are you using ? And more importantly which version of SensioFrameworkExtraBundle ? The @Method annotation from SensioFrameworkExtraBundle has been removed in latest version – Dylan KAS Apr 26 '19 at 06:54

2 Answers2

2

First of all, careful of which SensioFrameworkExtraBundle version you are using because the @Method annotation from SensioFrameworkExtraBundle has been removed in latest version. Instead, the Symfony @Route annotation defines a methods option to restrict the HTTP methods of the route:

*
* @Route("/show/{id}", methods={"GET","HEAD"})
*

But in your case, if you're using HTML forms and HTTP methods other than GET and POST, you'll need to include a _method parameter to fake the HTTP method.

See How to Change the Action and Method of a Form for more information.

Dylan KAS
  • 4,840
  • 2
  • 15
  • 33
  • ..... I'll have to check that, when did this happen? :X I admit it's been a few months since I've last created a fresh symfony project, but this thing didn't even give me a deprecation notification :( – RedactedProfile Apr 26 '19 at 15:32
0

I think you have to add route name and it must be unique.

Try with following way:

/**
     * @Route("/{game}",name="api_remove")
     * @Method({"DELETE"})
     */
    public function remove(Request $request, Game $game) {
      ...
    }

    /**
     * @Route("/{game}",name="single_remove")
     * @Method({"GET"})
     */
    public function single(Request $request, GameSerializer $gameSerializer, Game $game) {
       ...
    }

Mitesh Vasava
  • 707
  • 6
  • 13