1

我有一个登录过程的操作。现在我想对正常登录过程和 ajax 请求使用相同的操作。

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

我希望此操作为 xmlhttp 请求和普通 http 请求呈现不同的视图文件。我该怎么做?我想在 json 对象中传递视图文件

4

1 回答 1

1

你可以这样做:

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

或者

if ($this->getRequest()->isXmlHttpRequest()){
  $template = "form.html.twig" ;
  $params = ....
} else {
  $template = "login.html.twig" ;
  $params = ....
}

return $this->render($template, $params) ;
于 2012-09-11T09:18:10.983 回答