Silex2: Security firewall and locale

僤鯓⒐⒋嵵緔 提交于 2019-12-12 04:42:42

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/46155720/silex2-security-firewall-and-locale

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