gpt4 book ai didi

symfony - 重定向已登录用户 Symfony2

转载 作者:行者123 更新时间:2023-12-01 10:02:17 25 4
gpt4 key购买 nike

有没有办法将已经登录的用户重定向到主页?例如,那些带有 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] }

最佳答案

您在重定向行中错过了“返回”

$securityContext = $this->container->get('security.context');
if($securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')){
return $this->redirect($this->generateUrl('MonsePanelBundle_homepage'));
}

关于symfony - 重定向已登录用户 Symfony2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14542940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com