Announcement

Collapse
No announcement yet.

How to get the current user in PHP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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?

  • #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


    • #3
      You should use ->getId() instead of ->id. I highly recommend using IDE that will highlight deprecation usage as well as other issues.

      Comment


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

        Comment

        Working...
        X