Silex 2 : Security firewall error with locale (Silex SecurityServiceProvider + Pmaxs\\LocaleServiceProvider)

谁说胖子不能爱 提交于 2019-12-02 00:13:52

I think I manage to solve my problem this way :

1/ To dodge my error 404 where I login and logout when I have another locale than en, I redirect the user in en before my check_path and logout :

// The login form action
<form role="form" action="{{ locale_generate('en', 'login_check') }}" method="post">

// The logout
<a href="{{ locale_generate('en', 'logout') }}"> <span class="log">{{ 'logout'|trans }}</span></a>

This way it works but user came back in english all the time, so I have to redirect him in my page but with the same locale than before.

2/ So I add the selected langage in session (if I have no $_SESSION['langue'], I use my default locale as value : en)

//===============================================
// My Ajax call where I send the selected langage value
//===============================================
$(".change_langue").on("click", function() {
    var new_langue = $(this).data("lg");

    $.ajax({
        url: "{{ path('new-langue') }}",
        type: 'POST',
        data: {'langue': new_langue},
        success: function (resp) {
            console.log(resp);
        },
        error: function (resp) {
            console.log(resp);
        }
    });
});

//========================================================
// My controller where I set the value in session
//========================================================
$app->post('/new-langue', function(Request $request) use ($app) {
    $new_langue = $request->get('langue');

    $app['session']->set('langue', $new_langue);
    $result['new_langue'] =  $app['session']->get('langue');

    return new Response(json_encode($result));

})->bind('new-langue');

3/ Then I change my default login path when I registered my SecurityProvider :

$app->register(new Silex\Provider\SecurityServiceProvider(), array(
    'security.firewalls' => array(
        'secured' => array(
            'pattern' => '^/',
            'anonymous' => true,
            'form' => array(
                'login_path' => 'login',
                'check_path' => '/login_check',
                'always_use_default_target_path' => true,  // I add this line
                'default_target_path' => '/login/redirect' // I add this line
            ),
            'logout' => array(
                'logout_path' => '/logout',
                'invalidate_session' => true,                      
            ),
            'users' => function () use ($app) {
                return new _mypixers_silex\DAO\UserDAO($app['db']);
            },
        ),
    ),
));

4/ And now in my new /login/redirect controller I check the $_SESSION['langue'] value and redirect to the right langage :

//========================================================
// LOGIN REDIRECT
//========================================================
$app->get('/login/redirect', function(Request $request) use ($app) {
    $session_langue = $app['session']->get('langue');
    if (empty($session_langue)) {
        $session_langue = 'en';
    }
    return $app->redirect($app['locale.url_generator']->generate($session_langue, 'pixers'));

})->bind('login_redirect');

This way I have no more error when I login or logout with an other locale than en and the user stay in his current langage.

PS : I still come back to english when I logout right now, but I'm working on a solution. I will edit my anwser when it's done

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!