有沒有人已經在Symfony 2和FOS User Bundle的Bootstrap模式下創建了一個登錄表單?Symfony 2 FOS用戶捆綁Bootstrap模態AJAX登錄
這是我現在有:
的src/Webibli/UserBundle /資源/配置/ service.yml
authentication_handler:
class: Webibli\UserBundle\Handler\AuthenticationHandler
arguments: [@router, @security.context, @fos_user.user_manager, @service_container]
應用程序/配置/ security.yml
form_login:
provider: fos_userbundle
success_handler: authentication_handler
failure_handler: authentication_handler
src/Webibli/UserBundle/Handler/AuthenticationHandler.php
<?php
namespace Webibli\UserBundle\Handler;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
class AuthenticationHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
{
protected $router;
protected $security;
protected $userManager;
protected $service_container;
public function __construct(RouterInterface $router, SecurityContext $security, $userManager, $service_container)
{
$this->router = $router;
$this->security = $security;
$this->userManager = $userManager;
$this->service_container = $service_container;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token) {
if ($request->isXmlHttpRequest()) {
$result = array('success' => true);
$response = new Response(json_encode($result));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
else {
// Create a flash message with the authentication error message
$request->getSession()->getFlashBag()->set('error', $exception->getMessage());
$url = $this->router->generate('fos_user_security_login');
return new RedirectResponse($url);
}
return new RedirectResponse($this->router->generate('anag_new'));
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) {
if ($request->isXmlHttpRequest()) {
$result = array('success' => false, 'message' => $exception->getMessage());
$response = new Response(json_encode($result));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
return new Response();
}
}
這裏是嫩枝觀點我加載到我的引導模式:
{% extends 'UserBundle::layout.html.twig' %}
{% trans_default_domain 'FOSUserBundle' %}
{% block user_content %}
<script>
$('#_submit').click(function(e){
e.preventDefault();
$.ajax({
type : $('form').attr('method'),
url : $('form').attr('action'),
data : $('form').serialize(),
success : function(data, status, object) {
console.log(status);
console.log(object.responseText);
}
});
});
</script>
<div class="modal-dialog">
<div class="modal-content">
<form action="{{ path("fos_user_security_check") }}" method="post" role="form" data-async data-target="#rating-modal" class="text-left">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">{{ 'layout.login'|trans }}</h4>
</div>
<div class="modal-body">
{% if error %}
<div>{{ error|trans }}</div>
{% endif %}
<input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />
<div class="form-group container">
<label for="email">{{ 'security.login.username_email'|trans }}</label>
<input type="text" class="form-control" id="username" name="_username" value="{{ last_username }}" required="required" placeholder="[email protected]">
</div>
<div class="form-group container">
<label for="password">{{ 'security.login.password'|trans }}</label><br />
<input type="password" id="password" name="_password" required="required" class="form-control" placeholder="********">
</div>
<div class="form-group container">
<label for="remember_me">
<input type="checkbox" id="remember_me" name="_remember_me" value="on" />
{{ 'security.login.remember_me'|trans }}
</label>
</div>
</div>
<div class="modal-footer">
<input type="submit" id="_submit" name="_submit" value="{{ 'security.login.submit'|trans }}" class="btn btn-primary">
</div>
</form>
</div>
</div>
{% endblock %}
的登錄表單不AJAX工作完全正常。我只是試圖在模式中出現錯誤,如果出現問題,或者在登錄成功時重定向用戶。
任何人都可以解釋如何做到這一點?
AJAX無法正常工作,您可以嘗試執行錯誤回調以查看從服務器獲得的響應(如果有)。 – joe42
我總是成功,因爲請求正在工作,但登錄不成功,我不知道如何讓Symfony在JSON中返回錯誤消息而不是Flash消息。目前,AJAX調用的響應總是一個完整的HTML頁面,這是令人討厭的,因爲我只想要在模式中應用的內容。 – f0x7ed