简体   繁体   English

Symfony2-注册后重定向到登录页面

[英]Symfony2 - redirecting to login page after register

i have problem with fosuserbundle. 我对fosuserbundle有问题。 I created a custom register form and action but it works only when I'm logged in 我创建了一个自定义注册表单和操作,但是仅当我登录后才有效

/**
 * @Route("/account/register", name="register_account")
 */
public function registerAction(Request $request) {

    $user = new User();

    $form = $this->createForm(new RegistrationType, $user);

    $form->handleRequest($request);

    /*$pwd = $user->getPassword();
    $encoder = $this->container->get('security.password_encoder');
    $pwd = $encoder->encodePassword($user, $pwd);
    $user->setPassword($pwd);*/

    if ($form->isValid()) {


    $user->addRole(User::ROLE_USER);
    $user->setEnabled(true);

        $this->addFlash('notice', 'Dodano!');
        $em = $this->getDoctrine()->getManager();
        $em->persist($user);
        $em->flush();

        return $this->redirectToRoute('register_account');
    }
    return $this->render('default/register.html.twig', ['form' => $form->createView() ]);
}

and when I'm not logged in it redirects me to login page i simply don't know where i made mistake 当我没有登录时,它会将我重定向到登录页面,我根本不知道我在哪里犯了错误

You have made a little mistake: When your form is submitted and validated, you redirect to the same route of your initial action register_account . 您犯了一个小错误:提交并验证表单后,您将重定向到初始操作register_account的相同路径。

Try something like that: 尝试这样的事情:

/.../

return $this->redirectToRoute('succes_page');

/.../   

Where succes_page matches another action of your controller. succes_page与控制器的另一个动作匹配的地方。

It's because /account/register url is under security firewall . 这是因为/account/register网址位于security firewall

You should make this url accessible without being logged in. 您应该使该URL可访问而无需登录。

On your security.yml file add following lines: 在您的security.yml文件上,添加以下行:

security:
...
    firewalls:
    ...
        login_firewall:
            pattern:   ^/register/account
            anonymous: ~
        ...        
    access_control:
        - { path: ^/register/account, roles: IS_AUTHENTICATED_ANONYMOUSLY }

Also you should change the redirect route to something else not the same registration route again. 另外,您应该再次将重定向路由更改为其他不同于同一注册路由的路由。 It's an infinite loop. 这是一个无限循环。

This route name should be something else lie success page or home page. 此路由名称应该是成功页面或主页上的其他内容。

return $this->redirectToRoute('register_account');

You can always get more information about Symfony's security component here . 您始终可以在此处获得有关Symfony安全组件的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM