Silex2: Security firewall and locale

2019-08-18 05:47发布

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,
       ),
    ),
 );

标签: symfony silex
1条回答
The star\"
2楼-- · 2019-08-18 06:40

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,
    )
);
查看更多
登录 后发表回答