在 Silex Framework 中使用 ajax

Use of ajax in Silex Framework

我是 Silex Framework 的新手,我想知道如何进行简单的登录(使用 SecurityServiceProvider)ajax 请求。在我的代码中一切正常(见下文)但是 我如何更改 html 页面返回的布尔值,无论登录是否有效.

app.php

use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\Debug\ExceptionHandler;

// Register global error and exception handlers
ErrorHandler::register();
ExceptionHandler::register();

// Register service providers
$app->register(new Silex\Provider\DoctrineServiceProvider());
$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => __DIR__ . '/../views',
));
$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
$app->register(new Silex\Provider\SessionServiceProvider());
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
    'security.firewalls' => array(
        'secured' => array(
            'pattern' => '^/',
            'anonymous' => true,
            'logout' => array('logout_path' => '/admin/logout', 'invalidate_session' => true),
            'form' => array('login_path' => 'login', 'check_path' => '/login_check'),
            'users' => $app->share(function () use ($app) {
                return new ski\DAO\MemberDAO($app['db']);
            }),
        ),
    ),
));

// register services
$app['dao.member'] = $app->share(function ($app) {
    return new ski\DAO\MemberDAO($app['db']);
});

routes.php

use Symfony\Component\HttpFoundation\Request;

// Home page
$app->get('/', function () use ($app) {
    return $app['twig']->render('index.html.twig');
})->bind('home');

// TODO : never called
$app->post('/ajax/login/', function (Request $request) use ($app) {
    // HERE : how to return if the login was performed well ?
    return $app['security.last_error']($request);
})->bind('ajax_login');

$app->get('/login/', function (Request $request) use ($app) {
    return $app['twig']->render('login.html.twig', array(
        'error' => $app['security.last_error']($request),
        'last_username' => $app['session']->get('_security.last_username'),
    ));
})->bind('login');

$app->get('/includes/header/', function () use ($app) {
    return $app['twig']->render('header.html.twig');
})->bind('header');

和login.js

// Connexion
$(document).on('click', '#connexion_submit_button', function () {
    // Connexion Ajax
    var username = $('#connexion_pseudo').val();
    var password = $('#connexion_password').val();

    $.ajax({
        type: 'POST',
        url: '/ski/web/login_check',
        data: '_username=' + username + '&_password=' + password,
        beforeSend: function () {
            $('#connexion_submit_button').html('Patientez...');
        },
        success: function (data) {

            // TODO : generate custom animations if user is logged or not
            console.log(data);
            $('#connexion_submit_button').html('Connexion'); 
        }
    });
    return false;
});

如有任何建议,我们将不胜感激!

顺便问一下,在这样的框架中 ajax 有什么好的做法吗?

Silex 在后台使用 Symfony 的安全组件,SecurityServiceProvider sets $app['dispatcher'] as event dispatcher to AuthenticationProviderManager. I guess this means that it'll fire events as listed in Security Component documentation

对您来说有趣的应该是 security.interactive_loginsecurity.authentication.failure。两者都会触发一个事件,您可以在其中完全访问 Request 对象,您可以在其中修改匹配的控制器或通过使用 EventSubscriberInterface.

订阅事件来做任何您想做的事情

身份验证成功和失败处理程序创建和管理响应。因此,如果您想进行自定义响应,则需要自定义身份验证成功和失败处理程序。

添加处理程序

MyAuthenticationFailureHandler

use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler as BaseDefaultAuthenticationFailureHandler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\Exception\AuthenticationException;

class MyAuthenticationFailureHandler extends BaseDefaultAuthenticationFailureHandler
{
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        if ($request->isXmlHttpRequest()) return new JsonResponse(['login' => false]);
        return parent::onAuthenticationFailure($request, $exception);
    }
}

MyAuthenticationSuccessHandler

use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler as BaseDefaultAuthenticationSuccessHandler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class MyAuthenticationSuccessHandler extends BaseDefaultAuthenticationSuccessHandler
{
    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        if ($request->isXmlHttpRequest()) return new JsonResponse(['login' => true]);
        return parent::onAuthenticationSuccess($request, $token);
    }
}

并在安全服务提供商中注册它们

$app['security.authentication.success_handler.secured'] = $app->share(function () use ($app) {
    $handler = new MyAuthenticationSuccessHandler(
        $app['security.http_utils'],
        $app['security.firewalls']['secured']['form'] //$options,
    );
    $handler->setProviderKey('secured');

    return $handler;
});

$app['security.authentication.failure_handler.secured'] = $app->share(function () use ($app) {
    return new MyAuthenticationFailureHandler(
        $app,
        $app['security.http_utils'],
        $app['security.firewalls']['secured']['form'],
        $app['logger']
    );
});