Question

how could i getState from zfcUser

in view/index.phtml i get it from $this->zfcUserIdentity()->getState();

but now i need to get this value ( state for this user who is logged in ), in other module /controller (this is my costum module controller)

so i need to get State from: zfcUser/Entity/User to myModule/Controller

i watch this https://github.com/ZF-Commons/ZfcUser/wiki/How-to-check-if-the-user-is-logged-in but this solutons is not helpful

Was it helpful?

Solution

and this helps too, for me:

$sm = $this->getServiceLocator();
$auth = $sm->get('zfcuserauthservice');
if ($auth->hasIdentity()) {
    $user_edit = $auth->getIdentity()->getPrem();
}

OTHER TIPS

The state is a property from the user itself. So if you get the user throught the identification service, you can grab the state from there.

public function myFooAction()
{
    if ($this->zfcUserAuthentication()->hasIdentity()) {
        $user  = $this->zfcUserAuthentication()->getIdentity();
        $state = $user->getState();
    }
}

Mind that when the user is not logged in, the if condition is false. Also the state can be null or any arbitrary value, so do not expect that every user returns a valid state (in other words, check the returned value!)

follow this code, I had same problem then I have manage how to use identity of logged in user via zfcUser

in other modules controller at topside,

  use Zend\EventManager\EventManagerInterface;

then create this two function in the sameclass,

public function setEventManager(EventManagerInterface $events)
{
     parent::setEventManager($events);

    $controller = $this;
    $events->attach('dispatch', function ($e) use ($controller) {

        if (is_callable(array($controller, 'checkUserIdentity')))
        {
            call_user_func(array($controller, 'checkUserIdentity'));
        }
    }, 100);
}

public function checkUserIdentity()
{

    if ($this->zfcUserAuthentication()->hasIdentity()) {
    echo "<pre>"; print_r($this->zfcUserAuthentication()->getIdentity());die;

        }

}

it will give this kind of output

Admin\Entity\User Object
(
[id:protected] => 2
[username:protected] => 
[email:protected] => rajat.modi@softwebsolutions.com
[displayName:protected] => 
[password:protected] => $2y$14$2WxYLE0DV0mH7txIRm7GkeVJB3mhD4FmnHmxmrkOXtUFL7S9PqWy6
[state:protected] => 
)

That's it you will automatically get Identity whether user is logged in if not then it will redirect to login page.

Hope this will helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top