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
1 Answer 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
-
But how to render the template inside a json as OP mentionned?Adib Aroui– Adib Aroui2015年04月29日 14:01:51 +00:00Commented Apr 29, 2015 at 14:01
-
This maybe will help others to render view as part of a json stackoverflow.com/questions/13257156/…Adib Aroui– Adib Aroui2015年04月29日 14:04:50 +00:00Commented Apr 29, 2015 at 14:04
lang-php
json_encode(array('output'=> rendertemplate()))