【问题标题】:use SessionLogoutHandler to add data in database使用 SessionLogoutHandler 在数据库中添加数据
【发布时间】:2017-10-08 06:35:51
【问题描述】:

我有一个 symfony 应用程序,其中注销 () 不是手动的:

(在文件 routing.yml 中)

logout:
pattern:   /logout

我希望当我点击 /logout 时,我们必须能够在数据库中记录信息,并且我会使用:

SessionLogoutHandler class

这是我的代码:

namespace Symfony\Component\Security\Http\Logout;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

use Compta\MyappliBundle\Entity\LogTache;
use Compta\MyappliBundle\Entity\User;
use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class SessionLogoutHandler implements LogoutHandlerInterface
{
    public function logout(Request $request, Response $response, TokenInterface $token)
    {

            $em = $this->getDoctrine()->getEntityManager();
            $user= $this->get('security.context')->getToken()->getUser();
            $u = new LogTache();

            $u->setDate(new \DateTime());
            $u->setUser($user->getUsername());
            $u->setActioneffectue('Déconnexion');

            $em->flush($u);

    $request->getSession()->invalidate();

    } }

当我点击 /logout 时出现错误:

致命错误:调用未定义的方法 Symfony\Component\Security\Http\Logout\SessionLogoutHandler::getDoctrine() 在 C:\wamp\www\MyAppli\vendor\symfony\src\Symfony\Component\Security\Http\Logout\SessionLogoutHandler.php 第 46 行

如何解决? 提前感谢

请原谅我的英语

【问题讨论】:

    标签: symfony


    【解决方案1】:

    您无权从该类访问容器。 $this->getDoctrine()...$this->get() 更有可能出现在控制器操作中。

    您可以添加一个构造函数并传入“@doctrine”和“@security.context”作为服务定义中的参数,或者使用 Symfony 3.3 类型提示适当的类(对于 Doctrine - Doctrine\Common\Persistence\ObjectManager)并使用它们在课堂上。

    Symfony 2.6 弃用了 security.context 服务,因此它也引用了您可能正在运行旧版本的框架。升级可能会带来额外的好处,例如 v3.3+ 服务的自动装配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多