Plugin for retrieving the current authenticated identity within zend-mvc controllers.
$ composer require zendframework/zend-mvc-plugin-identity
Install via composer:
$ composer require zendframework/zend-mvc-plugin-identity
If you are using the zend-component-installer, you're done!
If not, you will need to add the component as a module to your
application. Add the entry 'Zend\Mvc\Plugin\Identity'
to
your list of modules in your application configuration (typically
one of config/application.config.php
or
config/modules.config.php
).
The Identity
plugin allows retrieving the identity from the
AuthenticationService
.
For the Identity
plugin to work, a
Zend\Authentication\AuthenticationService
name or alias
must be defined and recognized by the ServiceManager
.
Identity
returns the identity in the
AuthenticationService
or null
if no identity
is available.
As an example:
public function testAction()
{
if ($user = $this->identity()) {
// someone is logged !
} else {
// not logged in
}
}
When invoked, the Identity
plugin will look for a service
by the name or alias
Zend\Authentication\AuthenticationService
in the
ServiceManager
. You can provide this service to the
ServiceManager
in a configuration file:
// In a configuration file...
use Zend\Authentication\AuthenticationService;
return [
'service_manager' => [
'aliases' => [
AuthenticationService::class => 'my_auth_service',
],
'invokables' => [
'my_auth_service' => AuthenticationService::class,
],
],
];
The Identity
plugin exposes two methods:
setAuthenticationService(AuthenticationService $authenticationService) : void
:
Sets the authentication service instance to be used by the plugin.
getAuthenticationService() : AuthenticationService
:
Retrieves the current authentication service instance if any is
attached.