2012-07-31 90 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()) 

什么引起我的头疼。

相关问题