2012-07-31 85 views
2

所以我想用一個登錄(登錄)表單和註冊(註冊)形式與Symfony2的頁面。我想知道什麼是實現這個目標的最好方法?Symfony2在一個頁面上的多種形式

我考慮過設置表格屬性action="/pathToMyFormProcess",但是接着我遇到了一個問題:我想在該動作中顯示相同的模板(HTML頁面)。但是「/ pathToMyFormProcess」將在不同模板的不同動作中被調用。就像在每個頁面上包含或呈現的登錄表單一樣。我可以爲每個模板創建一個新的路徑和行動。但正如這個例子表明這將是非常令人沮喪的:

我註冊的行動將是:

public function signupAction() { 

    $loginForm = $this->get('loginform'); // Get the login form 
    $signUpForm = $this->get('signupform'); // Get the signup form 

    $loadData = ... /* Loading data needed for this page (could be last visitors, 
    countries, some information from database that should be displayed on the page) */ 

    return // SIGNUPTEMPLATE - array (forms and data) 

} 

這時如果有人使用的登錄表單,將它們發送到例如/註冊/登錄用行動在loginAction中例如SignupController。

public function signupAction() { 

    $loginForm = $this->get('loginform'); // Get the login form 
    $handler = $this->get('loginhandler'); // Get the handler 

    $process = $handler->process(); // Process the request, try to log in basicly. 
    if($process) { 
     return // redirect or something - no problem 
    } 

    // Loading again, writing exact same code as signupAction() 
    $signUpForm = $this->get('signupform'); // Get the signup form 

    $loadData = ... 

    return // SIGNUPTEMPLATE - array (forms and data) 
} 

所以我想知道如果你有什麼更好的想法如何實現這一目標?

回答

1

另一件事,你可以做ES呈現在您的網頁形式,例如:

在layout.html.twig

{{ render(controller('SiteBundle:Login:login')) }} 

然後在您的loginAction呈現您的登錄表單。

return $this->render('SiteBundle:Default:login-box.html.twig', array(
     'last_username' => $lastUsername, 
     'error'   => $error, 
     'csrf_token' => $csrfToken, 
    )); 

最後你的登錄box.html.twig有你的HTML格式。

0

小心不同的分支機構。 我發現沒有任何理由SYMFONY的新版本中出現了一些奇怪的變化。例如分支2.6

public FormBuilderInterface createNamedBuilder(string|int $name, string|FormTypeInterface $type = 'form', mixed $data = null, array $options = array()) 

但在分支2.0

public FormBuilder createNamedBuilder(string|FormTypeInterface $type, string $name, mixed $data = null, array $options = array()) 

什麼引起我的頭疼。

相關問題