我想有兩種不同的(我指的是不同的模板)表單,可以在不同的樹枝中登錄我的用戶。但我從我的一個表單中得到一個錯誤...這就是我一步一步做的:Symfony2 FOSUser/SonataBundle無效的CSRF令牌值
我有路線/login
上的主要登錄模板。 此登錄帳號可以使用!。
要在/product_frm
第二種形式的工作,我這樣做:)創造了第二次登錄控制器的路線
1。所以現在我有兩個登錄路線:
login:
path: /login
defaults: { _controller: ApplicationSonataUserBundle:SecurityFOSUser1:login }
loginForm:
path: /login_frm
defaults: { _controller: ApplicationSonataUserBundle:LoginFormType:login }
現在,這是爲/login_frm
這是相同的/login
控制器控制器,只呈現模板是不同的:
<?php
/*
* This file is part of the Sonata package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Application\Sonata\UserBundle\Controller;
use FOS\UserBundle\Controller\SecurityController;
use Sonata\UserBundle\Model\UserInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Sonata\UserBundle\Controller\SecurityFOSUser1Controller as BaseController;
class LoginFormTypeController extends BaseController
{
public function loginAction()
{
$user = $this->container->get('security.context')->getToken()->getUser();
if ($user instanceof UserInterface) {
$this->container->get('session')->getFlashBag()->set('sonata_user_error', 'sonata_user_already_authenticated');
$url = $this->container->get('router')->generate('sonata_user_profile_show');
return new RedirectResponse($url);
}
return parent::loginAction();
}
public function renderLogin(array $data)
{
$template = sprintf('MpShopBundle:Form:login_form2.html.%s', $this->container->getParameter('fos_user.template.engine'));
return $this->container->get('templating')->renderResponse($template, $data);
}
}
最後,我製作加載表單的模板:
{% block fos_user_content %}
<div class="span9">
<div class="well">
{% block sonata_user_login %}
<div class="panel panel-info">
<div class="panel-heading">
<h2 class="panel-title">{{ 'title_user_authentication'|trans({}, 'SonataUserBundle') }}</h2>
</div>
<div class="panel-body">
{% block sonata_user_login_error %}
{% if error %}
<div class="alert alert-danger alert-error">{{ error|trans({}, 'FOSUserBundle') }}</div>
{% endif %}
{% endblock %}
{% block sonata_user_login_form %}
<form action="{{ path("fos_user_security_check") }}" method="post" role="form"
class="form-horizontal">
<input type="hidden" name="_csrf_token" value="{{ csrf_token }}"/>
<div class="control-group">
<label class="control-label control-label required" for="username"
class="col-sm-4 control-label">{{ 'security.login.username'|trans({}, 'SonataUserBundle') }}</label>
<div class="controls">
<input type="text" class="form-control" id="username" name="_username" value="{{ last_username }}" required="required"/></div>
</div>
<div class="form-group control-group">
<label class="control-label control-label required" for="password"
class="col-sm-4 control-label">{{ 'security.login.password'|trans({}, 'SonataUserBundle') }}</label>
<div class="controls">
<input type="password" class="form-control" id="password" name="_password" required="required"/></div>
</div>
<div class="control-group">
<div class="col-sm-offset-4 col-sm-8">
<div class="checkbox control-group">
<label class="checkbox pull-left" for="remember_me">
<input type="checkbox" id="remember_me" name="_remember_me" value="on"/>
{{ 'security.login.remember_me'|trans({}, 'FOSUserBundle') }}
</label>
</div>
</div>
</div>
<div class=" control-group pull-left">
<div class="">
<a href="{{ path('forgetpass') }}">{{ 'forgotten_password'|trans({}, 'SonataUserBundle') }}</a>
</div>
</div>
<div class="form-actions">
<div class="pull-left">
<input type="submit" id="_submit" name="_submit" class="btn btn-primary pull-right"
value="{{ 'security.login.submit'|trans({}, 'FOSUserBundle') }}"/>
</div>
</div>
</form>
{% endblock %}
</div>
</div>
{% endblock %}
</div>
</div>
{% endblock fos_user_content %}
The ful l錯誤:如果我嘗試登錄/login_frm
,我得到Invalid CSRF token.
錯誤。如果我從/login
登錄,它工作正常。這是爲什麼?不能我有兩種形式登錄?
[CSRF令牌無效。請嘗試重新提交表單](http://stackoverflow.com/questions/23455780/the-csrf-token-is-invalid-please-try-to-resubmit-the-form) –
它不是重複的,ty: ) – Dominykas55