Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current user object in class without create Service

After login, I want current user object in my class without creating service. Becauase I had already create current user pluggin and helper.

So If i need current user object in my class then How to get current user object.

i.e,

class CoreFunction
{
    public static curentUserData()
    {
       // i want user object here
    }

}

In Zend Framework 2, I just used:

class CoreFunction
{
    public static curentUserData() {         
        $authService = $this->getServiceLocator()
                    ->get('Zend\Authentication\AuthenticationService')
                    ->getIdentity();
    }
}

How can I do this in Zend Framework 3?

like image 680
Ohm Avatar asked Aug 15 '26 19:08

Ohm


1 Answers

As in ZF2, you should have access to controller plugin identity and use it like $this->identity()

// From the doc.
public function testAction()
{
    if ($user = $this->identity()) {
         // someone is logged !
    } else {
         // not logged in
    }
}

If you want to use it in other class than controller, than you need to inject it, as in ZF2.

like image 172
tasmaniski Avatar answered Aug 25 '26 15:08

tasmaniski