Search code examples
symfonysilex

Silex2: Security firewall and locale


How do I add the current locale to paths like /user/login or /user/logout? Controllers do support the '{_locale}' placeholder, but within the security pattern it is reported as an error.

$app['security.firewalls'] = array(
    'login' => array(
        'pattern' => '^/user/login$',
    ),
    'secured_area' => array(
        'pattern' => '^.*$',
        'anonymous' => false,
        'remember_me' => array(),
        'form' => array(
           'login_path' => '/user/login',
           'check_path' => '/user/login_check',
        ),
       'logout' => array(
           'logout_path' => '/user/logout',
           'invalidate_session' => true,
       ),
    ),
 );

Solution

  • The solution was to use the route name (controller bind) in 'login_path', not the full path.

    $app->get('/{_locale}/user/login', function(Request $request) use ($app) {
        return $app['twig']->render('login.html.twig', array(
            'error'         => $app['security.last_error']($request),
        ));
    })->bind('login');
    
    
    $app['security.firewalls'] = array(
        'login' => array(
            'pattern' => '^/(de|en|fr|es)/user/login$',
        ),
        'main' => array(
            'pattern' => '^.*$',
            'anonymous' => false,
            'remember_me' => array(),
            'form' => array(
            'login_path' => 'login',
            'check_path' => '/user/login_check',
            'post_only' => true,
            'with_csrf' => true,
            'default_target_path' => 'homepage'
        ),
        'logout' => array(
            'logout_path' => '/user/logout',
            'invalidate_session' => true,
        )
    );