-
Notifications
You must be signed in to change notification settings - Fork 341
How to check if the user is logged in
larson72 edited this page Nov 13, 2012
·
18 revisions
Check if the user is logged in (ie: user identity widget)
There are three ways.
ZfcUser provides a View Helper (zfcUserIdentity) which you can use from any view script in your application. Just add the following call to the location in your markup where you want the form to be rendered:
<?php echo $this->zfcUserIdentity(); ?>
You can also get user's fields (if the user is logged in), like email:
<?php echo $this->zfcUserIdentity()->getEmail(); ?>
The view helper may also return the Authentication Service :
<?php $authService = $this->zfcUserIdentity()->getAuthService(); ?>
ZfcUser provides a Controller Plugin (zfcUserAuthentication) which you can use from any controller in your application. You can check if the user is connected and get his data:
<?php
if ($this->zfcUserAuthentication()->hasIdentity()) {
//get the email of the user
echo $this->zfcUserAuthentication()->getIdentity()->getEmail();
//get the user_id of the user
echo $this->zfcUserAuthentication()->getIdentity()->getId();
//get the username of the user
echo $this->zfcUserAuthentication()->getIdentity()->getUsername();
//get the display name of the user
echo $this->zfcUserAuthentication()->getIdentity()->getDisplayname();
}
?>
$sm = $app->getServiceManager();
$auth = $sm->get('zfcuser_auth_service');
if ($auth->hasIdentity()) {
echo $auth->getIdentity()->getEmail();
}