Secure method using annotations Secure method using annotations symfony symfony

Secure method using annotations


You could do something like this.

You can allow both method types anonymously, and check just inside the controller to see if the user is authenticated and is POSTing.

(You don't state which version of symfony you're using, so you might have to substitute the authorization_checker (2.8) for the older security.context service)

/** * @param Request $request * @return Response * * @Route("/someroute", name="something") * @Method(methods={"POST", "GET"}) */public function calculatorAction(Request $request){    if ( !$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY') && $request->getMethod() == 'POST') {        throw new AccessDeniedHttpException();    }    $form=$this->createForm(new CallRequestType(),$callReq=new CallRequest());    $form->handleRequest($request);    // you also need to check submitted or youll fire the validation on every run through.    if($form->isSubmitted() && $form->isValid()){        //blabla    }    return $this->render('MyBundle:Pages:calculator.html.twig', array('form' => $form));}