如何在控制器中正确记录用户?

时间:2015-07-28 09:24:55

标签: php symfony

我有一个问题是在控制器中正确记录用户。我使用Symfony 2.4 我这样做了

 $this->get('security.context')->setToken(NULL);
 $this->get('request')->getSession()->invalidate();

它根本不起作用。此代码将我重定向到主页,但我仍然是登录用户。我尝试了5次,它只工作了一次。一旦我被重定向到登录页面然后到主页。我没有在symfony文档中找到任何内容。

我试过这个 - 没有成功

 $this->get('security.context')->setToken(NULL);
    $this->get('request')->getSession()->invalidate();

    $response = new Response();
    $response->headers->clearCookie('REMEMBERME');

    return $this->render(
                '...index.html.twig',
                array(
                   ...
                ), $response);

我仍然是登录用户。 所以,我只想在不重定向的情况下将用户注销。  为什么symfony不能做这么基本的事情。

编辑: https://stackoverflow.com/a/22223566/2241432 我的问题是:我必须留在同一个控制器。我不能重定向到/ logout路由,我不能返回空响应。我在应用程序中有很多注册,因此yml配置无法解决我的问题。对于每次注册我需要不同的解决方案我只想破坏用户并显示正确的树枝(从不主页)。 我需要这个解决方案

   $this->get('security.context')->setToken(NULL);
    $this->get('request')->getSession()->invalidate();

    $response = new Response();
    $response->headers->clearCookie('REMEMBERME');

    return $this->render(
                '...index.html.twig',
                array(
                   ...
                ), $response);

有人可以解释为什么这不起作用?如何记录用户并在不同的控制器中显示正确的布局?

0 个答案:

没有答案
相关问题