1

I have an action for login process. Now I want to use the same action for normal login process and for ajax requests also.

 /**
 * @Route("/sponsor/login", name="sponsor_login", options={"expose"=true})
 * @Template("SponsorBundle::login.html.twig")
 * @return array
 */
 public function loginAction()
 {}

I want this action to render different view files for xmlhttp request and for normal http request.How do i do that? and I want to pass view file in a json object

asked Sep 11, 2012 at 7:32
3
  • You could also check this answer Commented Sep 12, 2012 at 5:48
  • But how did you managed to render the template inside a json? Something like json_encode(array('output'=> rendertemplate())) Commented Apr 29, 2015 at 14:03
  • This maybe will help others to render view as part of a json stackoverflow.com/questions/13257156/… Commented Apr 29, 2015 at 14:04

1 Answer 1

1

You can do this:

return $this->getRequest()->isXmlHttpRequest()
 ? $this->render(.... "form.html.twig" ....)
 : $this->render(... full page that will include the form ...) ;

or

if ($this->getRequest()->isXmlHttpRequest()){
 $template = "form.html.twig" ;
 $params = ....
} else {
 $template = "login.html.twig" ;
 $params = ....
}
return $this->render($template, $params) ;
answered Sep 11, 2012 at 9:18
2
  • But how to render the template inside a json as OP mentionned? Commented Apr 29, 2015 at 14:01
  • This maybe will help others to render view as part of a json stackoverflow.com/questions/13257156/… Commented Apr 29, 2015 at 14:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.