如果已从登录页面登录,则Symfony重定向

时间:2017-09-29 07:49:33

标签: php symfony redirect symfony-2.8

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

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

renderLogin()方法

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

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

use Symfony\Component\HttpFoundation\RedirectResponse;

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

您需要在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);
}
}

要重定向尝试访问注册页面的经过身份验证的用户,您需要更改``

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

奇怪的是,这还没有在 FOSUserBundle 中解决,无论如何我有两个处理这个问题的Github Gists:

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

登录:https://gist.github.com/teeyo/121e21b35d71a9ab4a8f321043b6f6cd

答案 1 :(得分:0)

在FOS securityController中(用于登录):将其添加到登录操作

if($session->get("_security_main"))
{
$route = $this->container->get('router')->generate('site_faim_homepage');
return new RedirectResponse($route);

    }