有没有办法将已经登录的用户重定向到主页?例如,那些带有 cookie 或“记住我”的人?
我在我的“loginController”中试过这个,但它不起作用
public function loginAction()
{
$request = $this->getRequest();
$session = $request->getSession();
// $user = new Usuario();
// get the login error if there is one
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
}
$securityContext = $this->container->get('security.context');
if( $securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED') ){
$this->redirect($this->generateUrl('MonsePanelBundle_homepage'));
}
return $this->render('UsuariosBundle:Security:login.html.twig', array(
// last username entered by the user
'last_username' => $session->get(SecurityContext::LAST_USERNAME),
'error' => $error,
));
}
我已经阅读了所有其他类似的问题,但到目前为止还没有运气......
这是我的 security.yml 以防万一……
security:
encoders:
Monse\UsuariosBundle\Entity\Usuario:
algorithm: sha512
encode-as-base64: true
iterations: 10
role_hierarchy:
ROLE_ADMIN: [ROLE_USER]
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
user_db:
entity: { class: Monse\UsuariosBundle\Entity\Usuario, property: usuario }
firewalls:
main:
pattern: /.*
provider: user_db
form_login:
login_path: /login
check_path: /login_check
remember_me: true
default_target_path: /panel
logout:
path: /logout
target: /login
remember_me:
key: MiClaveSegura
lifetime: 1800
path: /.*
domain: ~
security: true
anonymous: true
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/panel, roles: [ROLE_SUPER_ADMIN, ROLE_ADMIN, ROLE_USER] }