I'm having trouble displaying the logged-in username using view helper.
Module:
public function getViewHelperConfig() {
return array(
'invokables' => array(
'UserIdentity' => new View\Helper\UserIdentity()
)
);
}
UserIdentity:
use Zend\View\Helper\AbstractHelper;
use Zend\Authentication\AuthenticationService,
Zend\Authentication\Storage\Session as SessionStorage;
class UserIdentity extends AbstractHelper {
protected $authService;
public function getAuthService() {
return $this->authService;
}
public function __invoke($namespace = null) {
$sessionStorage = new SessionStorage($namespace);
$this->authService = new AuthenticationService;
$this->authService->setStorage($sessionStorage);
if ($this->getAuthService()->hasIdentity()) {
return $this->getAuthService()->getIdentity();
} else {
return false;
}
}
}
View:
$usuario = $this->UserIdentity('Usuario');