How to get the current user in PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bandtank
    Active Community Member
    • Mar 2017
    • 385

    How to get the current user in PHP

    I can't figure out how to get the current user in PHP. I see a function called getUser in the Espo core files, but I my code is running in a hook, so I need to somehow use dependency injection to get access to the current user object. How can I do this?
  • bandtank
    Active Community Member
    • Mar 2017
    • 385

    #2
    I figured it out after finding some old code I wrote:
    PHP Code:
    use Espo\Entities\User;
    
    class NewHook
    {
      public function __construct(
        private User $loggedInUser,
      ) {}
    
      ...
    
      private function getUserId() {
        return $this->loggedInUser->id;
      }
    }

    Comment

    • yuri
      Member
      • Mar 2014
      • 8624

      #3
      You should use ->getId() instead of ->id. I highly recommend using IDE that will highlight deprecation usage as well as other issues.
      If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

      Comment

      • bandtank
        Active Community Member
        • Mar 2017
        • 385

        #4
        Ok, thanks for the suggestion. I will see if I can figure out how to turn that on in VS Code.

        Comment

        Working...