所以我想用 Symfony2 制作一个带有登录(登录)表单和注册(注册)表单的页面。我想知道实现这一目标的最佳方法是什么?
我考虑过设置表单属性 action="/pathToMyFormProcess"
但我随后面临一个问题:我想在该操作中显示相同的模板(HTML 页面)。但是“/pathToMyFormProcess”会在使用不同模板的许多不同操作中被调用。就像在每个页面上包含或呈现的登录表单一样。我可以为每个模板创建一个新的路径和 Action 。但正如这个例子所示,这将非常令人沮丧:
我的注册操作是:
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 )
}
然后,如果有人使用登录表单,它会将他们发送到例如/signup/login,例如在 SignupController 中使用操作 loginAction。
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 )
}
所以我想知道您是否有更好的想法来实现这一目标?
最佳答案
您可以做的另一件事是在您的页面中呈现一个表单,例如:
在你的 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,
));
最后你的 login-box.html.twig 有你的 html 表单。
关于php - Symfony2 一页上的多个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11742487/