如果已经从登录页面登录,Symfony 重定向 [英] Symfony redirect if already loggedin from login page

查看:66
本文介绍了如果已经从登录页面登录,Symfony 重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 FOSUser Bundle 登录.现在,如果用户已经登录,如果用户访问/login url,我如何将用户重定向到主页('/').

我已将 SecurityController 复制到 src\AppBundle\Controller 位置并更改了 renderlogin 方法,但它不起作用.

renderLogin() 方法

受保护的函数 renderLogin(array $data){if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_ANONYMOUSLY')) {return new RedirectResponse('/', 403);}return $this->render('@FOSUser/Security/login.html.twig', $data);}

我也在安全控制器中添加了这一行,

使用Symfony\Component\HttpFoundation\RedirectResponse;

非常感谢任何帮助.

解决方案

好吧,您需要在 SecurityController

中进行一些更改

/*** 使用给定的参数呈现登录模板.改写这个函数* 一个扩展控制器,为登录模板提供额外的数据.** @param 数组 $data** @return 响应*/受保护的函数 renderLogin(array $data){/*** 如果用户已经登录(标记为由 symfony 的安全性完全验证)* 然后将此用户重定向回(在我的例子中,重定向到仪表板,这是* 我的登录用户)*/如果 ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {return $this->redirectToRoute('homepage');}return $this->render('@FOSUser/Security/login.html.twig', $data);}}

并且要重定向尝试访问注册页面的已验证用户,您需要更改``

class RegistrationController 扩展 BaseController{/*** @param 请求 $request** @return 响应*/公共函数 registerAction(Request $request){/*** 如果用户已经登录(标记为由 symfony 的安全性完全验证)* 然后将此用户重定向回(在我的例子中,重定向到仪表板,这是* 我的登录用户)*/如果 ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {return $this->redirectToRoute('homepage');}/** @var $formFactory FactoryInterface */$formFactory = $this->get('fos_user.registration.form.factory');/** @var $userManager UserManagerInterface */$userManager = $this->get('fos_user.user_manager');/** @var $dispatcher EventDispatcherInterface */$dispatcher = $this->get('event_dispatcher');$user = $userManager->createUser();$user->setEnabled(true);$event = new GetResponseUserEvent($user, $request);$dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);if (null !== $event->getResponse()) {返回 $event->getResponse();}$form = $formFactory->createForm();$form->setData($user);$form->handleRequest($request);如果 ($form->isSubmitted()) {如果 ($form->isValid()) {$event = new FormEvent($form, $request);$dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);$userManager->updateUser($user);if (null === $response = $event->getResponse()) {$url = $this->generateUrl('fos_user_registration_confirmed');$response = new RedirectResponse($url);}$dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));返回 $response;}$event = new FormEvent($form, $request);$dispatcher->dispatch(FOSUserEvents::REGISTRATION_FAILURE, $event);if (null !== $response = $event->getResponse()) {返回 $response;}}return $this->render('@FOSUser/Registration/register.html.twig', array('形式' =>$form->createView(),));}}

奇怪的是,这在 FOSUserBundle 中没有得到修复,无论如何我有两个 Github Gist 来处理这个问题:

注册:https://gist.github.com/teeyo/147f2d5d21d1beadce133a51b02d9570">

登录:https://gist.github.com/teeyo/121e21b35d781a9216a4b4>

I'm using FOSUser Bundle to login. Now if user is already loggedin, how can I redirect user to homepage ('/'), if user visit to /login url.

I have copied SecurityController to src\AppBundle\Controller location and changed renderlogin method but it doesn't work.

renderLogin() method

protected function renderLogin(array $data)
{
    if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_ANONYMOUSLY')) {
        return new RedirectResponse('/', 403);
    }
    return $this->render('@FOSUser/Security/login.html.twig', $data);
}

I have added this line as well in the security controller,

use Symfony\Component\HttpFoundation\RedirectResponse;

Any help is much appreciated.

解决方案

Well you need to make some changes in the SecurityController

 /**
 * Renders the login template with the given parameters. Overwrite this function in
 * an extended controller to provide additional data for the login template.
 *
 * @param array $data
 *
 * @return Response
 */
protected function renderLogin(array $data)
{
   /**
    * If the user has already logged in (marked as is authenticated fully by symfony's security)
    * then redirect this user back (in my case, to the dashboard, which is the main entry for 
    * my logged in users)
    */
   if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
       return $this->redirectToRoute('homepage');
   }
    return $this->render('@FOSUser/Security/login.html.twig', $data);
}
}

And to redirect authenticated users who try to visit the registration page, you need to change the ``

class RegistrationController extends BaseController
{
/**
 * @param Request $request
 *
 * @return Response
 */
public function registerAction(Request $request)
{
    /**
    * If the user has already logged in (marked as is authenticated fully by symfony's security)
    * then redirect this user back (in my case, to the dashboard, which is the main entry for 
    * my logged in users)
    */
    if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
       return $this->redirectToRoute('homepage');
    }
    /** @var $formFactory FactoryInterface */
    $formFactory = $this->get('fos_user.registration.form.factory');
    /** @var $userManager UserManagerInterface */
    $userManager = $this->get('fos_user.user_manager');
    /** @var $dispatcher EventDispatcherInterface */
    $dispatcher = $this->get('event_dispatcher');
    $user = $userManager->createUser();
    $user->setEnabled(true);
    $event = new GetResponseUserEvent($user, $request);
    $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);
    if (null !== $event->getResponse()) {
        return $event->getResponse();
    }
    $form = $formFactory->createForm();
    $form->setData($user);
    $form->handleRequest($request);
    if ($form->isSubmitted()) {
        if ($form->isValid()) {
            $event = new FormEvent($form, $request);
            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
            $userManager->updateUser($user);
            if (null === $response = $event->getResponse()) {
                $url = $this->generateUrl('fos_user_registration_confirmed');
                $response = new RedirectResponse($url);
            }
            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
            return $response;
        }
        $event = new FormEvent($form, $request);
        $dispatcher->dispatch(FOSUserEvents::REGISTRATION_FAILURE, $event);
        if (null !== $response = $event->getResponse()) {
            return $response;
        }
    }
    return $this->render('@FOSUser/Registration/register.html.twig', array(
        'form' => $form->createView(),
    ));
}
}

It's weird that this has not been fixed in the FOSUserBundle, anyway I had two Github Gists that handles this issue:

Registration : https://gist.github.com/teeyo/147f2d5d21d1beadce133a51b02d9570

Login : https://gist.github.com/teeyo/121e21b35d71a9ab4a8f321043b6f6cd

这篇关于如果已经从登录页面登录,Symfony 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆