A free and open-source book on ZF3 for beginners


16.9. Identity Controller Plugin and View Helper

One last thing we will discuss is how to check in your website if the user is logged in or not and retrieve the user identity. You can do that with the help of the Identity controller plugin and the Identity view helper.

To use the Identity plugin, you need to install zendframework/zend-mvc-plugins package with Composer, as follows:

php composer.phar require zendframework/zend-mvc-plugins

In your controller action method, you can check if user is logged in with the following code:

if ($this->identity()!=null) {
    // User is logged in
    
    // Retrieve user identity
    $userEmail = $this->identity();
}

In your view template, you can use Identity view helper for the same purpose:

// Echo user identity
<?= $this->escapeHtml($this->identity()) ?>

Top