2016-07-05 109 views
1

我想在同一頁面上顯示登錄和註冊表單。然而,似乎我們無法在控制器的不同的兩個動作中使用不同的表單變量來呈現相同的模板。使用Symfony 3在同一頁上呈現多個表單

這是我的意思;

class SecurityController extends Controller { 
/** 
* @Route("/", name="login") 
*/ 
public function loginAction(Request $request) 
{ 

    //Check if the user is already authenticated 
    if($this->container->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) { 
     return $this->redirect($this->generateUrl('dashboard')); 
    } 

    $authenticationUtils = $this->get('security.authentication_utils'); 

    // get the error if there is one 
    $error = $authenticationUtils->getLastAuthenticationError(); 

    // last username entered by the user 
    $lastUsername = $authenticationUtils->getLastUsername(); 

    $this->addFlash(
     'welcome', 
     'Welcome back!' 
    ); 

    return $this->render(
     '::landing.html.twig', 
     array(
      // last username entered by the user 
      'last_username' => $lastUsername, 
      'error'   => $error, 
     ) 
    ); 

    $registrationform = $this->get('form.factory')->createNamedBuilder('registrationform', UserType::class); 
} 

/** 
* @Route("/register", name="register") 
*/ 
public function registerAction(Request $request) 
{ 
    //Check authentication 
    if($this->container->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) { 
     return $this->redirect($this->generateUrl('dashboard')); 
    } 
    // get the authentication utils 
    $authenticationUtils = $this->get('security.authentication_utils'); 
    // get the login error if there is one 
    $error = $authenticationUtils->getLastAuthenticationError(); 
    // last username entered by the user 
    $lastUsername = $authenticationUtils->getLastUsername(); 
    // build the form 
    $user = new User(); 
    $form = $this->createForm(UserType::class, $user); 
     // handle the submit (will only happen on POST) 
     $form->handleRequest($request); 
     if ($form->isSubmitted() && $form->isValid()) { 
      $user->setRoles(array('ROLE_USER')); 
      // Encode the password (you could also do this via Doctrine listener) 
      $password = $this->get('security.password_encoder') 
       ->encodePassword($user, $user->getPlainPassword()); 
      $user->setPassword($password); 

      // save the User! 
      $em = $this->getDoctrine()->getManager(); 
      $em->persist($user); 
      $em->flush(); 

      $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles()); 
      $this->get('security.token_storage')->setToken($token); 
      $this->get('session')->set('_security_main', serialize($token)); 
      // ... do any other work - like sending them an email, etc 
      // maybe set a "flash" success message for the user 

      $this->addFlash(
       'success', 
       'Please complete your profile now.' 
      ); 

      $message = \Swift_Message::newInstance() 
       ->setSubject('You have successfully signed up!') 
       ->setFrom('[email protected]') 
       ->setTo($user->getUsername()) 
       ->setBody($this->renderView(
        ':emails:registration.html.twig'),'text/html'); 
      $this->get('mailer')->send($message); 

      return $this->redirectToRoute('update'); 
     } else{ 
      $errors = $this->get('validator')->validate($user); 
     } 

    return $this->render(
     '::landing.html.twig', 
     array('form' => $form->createView(), 
      'last_username' => $lastUsername, 
      'error' => $error, 
      ) 
    ); 
} 

}

上面我有我的登錄和註冊動作。我想將「表單」變量傳遞給我使用登錄操作呈現的相同的樹枝模板。當我嘗試這樣做時,我收到以下異常。

Variable "form" does not exist in ::landing.html.twig at line 50 
500 Internal Server Error - Twig_Error_Runtime 
+0

您沒有傳遞任何'form'變量在'loginAction'視圖。你注意到了嗎?這就是你得到錯誤的原因。 – Lumen

+0

是的。抱歉忘記在發佈之前刪除該更新。現在我更新了這個問題。我試圖在兩個操作中呈現相同的模板。 – mburakergenc

回答

2

我會做這樣的

2枝文件 -

login.html.twig和register.html.twig - 每個文件由他本人渲染動作

現在第三個樹枝文件叫做baseLogin.html.twig

在這個文件中,我通過調用渲染兩個文件(登錄和註冊)控制器

例如

baseLogin.html.twig

<div> 
    {{ render(controller('UserBundle:Security:login')) }} 
</div> 

<div> 
    {{ render(controller('UserBundle:Registration:register')) }} 
</div> 
+0

這是一個很好的方法,但我仍然無法在baseLogin.html.twig頁面上顯示錯誤。當用戶點擊登錄或註冊鏈接時,他們將被重定向到/ login或/ register頁面。我需要在baseLogin頁面上呈現所有操作。 – mburakergenc

+0

在窗體上設置動作optiin以編輯目標。 – Alsatian

相關問題