[Symfony] Restrict a route to the POST method via annotations

Published on 2018-09-26 • Modified on 2018-09-26

Sometimes you must restrict an action to be accessible with a given method only. That is the case for example for the "check_path" route used by the Symfony security component that mustn't be available with GET but only with the POST method.
If you don't restrict the route to the POST method and if you try to access the route via get with your browser you get a 500 error. Otherwise you have a HTTP_METHOD_NOT_ALLOWED error (code 405) which isn't a critical error and doesn't have to be handled by your application.


/**
 * @Route("/login_check", name="user_security_check", methods={"POST"})
 */
public function checkAction(): void
{
}

 More on Stackoverflow  Random snippet

  Work with me!