【问题标题】:Sentry Symfony How to capture user [duplicate]Sentry Symfony 如何捕获用户 [重复]
【发布时间】:2021-06-02 06:54:19
【问题描述】:

我在我的 symfony 项目中使用哨兵,我想识别有错误的用户。于是我查了一下哨兵文档发现->https://docs.sentry.io/platforms/php/guides/symfony/enriching-events/identify-user/

\Sentry\configureScope(function (\Sentry\State\Scope $scope): void {
  $scope->setUser(['email' => 'jane.doe@example.com']);
});

但我不知道把这个函数放在哪里,文档也没有提供相关信息。 我试图放入我的 index.php 文件,它可以工作,但我不知道如何获取当前用户的信息。

【问题讨论】:

    标签: php symfony sentry


    【解决方案1】:

    您可能会在处理身份验证的控制器操作中执行此操作。或者您在成功登录后重定向用户的任何地方。
    如果控制器扩展了Symfony\Bundle\FrameworkBundle\Controller\AbstractController,那么它将有一个getUser方法返回UserInterface|object|null

    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    
    class UserController extends AbstractController
    {
      /**
       * @Route("/user/profile", name="user_profile")
       */
      public function profile()
      {
        $user = $this->getUser();
        if($user){
          
          \Sentry\configureScope(function (\Sentry\State\Scope $scope): void {
            $scope->setUser(['username' => $user->getUsername()]);
          });
    
        }
    
        return $this->render('user/profile.html.twig', []);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-08
      • 1970-01-01
      • 2015-04-08
      • 2021-09-20
      • 2015-04-03
      • 2019-03-02
      • 1970-01-01
      • 2020-10-26
      相关资源
      最近更新 更多