Hello, recently I got a error of
Call to a member function getUsername() on null
in module.config.php
'service_manager' => [
'aliases' => [
'my_auth_service' => AuthenticationService::class,
],
'factories' => [
AuthenticationService::class => InvokableFactory::class,
],
],
in AdminTable.php which do the authentication logic
public function auth(Admin $admin)
{
$authAdapter = new AuthAdapter($this->tableGateway->getAdapter(), 'admin', 'username', 'passwd', 'SHA1(CONCAT(?, salt)) AND valid=1');
$authAdapter->setIdentity($admin->username);
$authAdapter->setCredential($admin->passwd);
$result = $authAdapter->authenticate();
$columnsToReturn = ['id','roleid','username','nickname'];
if ($result->isValid())
{
// .....
}
}
in the view add.phtml file (I wanna to test the identity helper):
<?php
$user = $this->identity();
echo $user->getUsername(); // ====> line 46
?>
Then I got a error reporting when browse the page.
An error occurred
An error occurred during execution; please try again later.Additional information:
Error
File: /opt/www/websites/mydomain.com/module/Application/view/application/role/add.phtml:46
Message: Call to a member function getUsername() on null
As the document says it will store something in some session.
Intro - zend-authentication - Zend Framework Docs in which the following annotation:
// Authentication succeeded; the identity ($username) is stored
// in the session:
// $result->getIdentity() === $auth->getIdentity()
// $result->getIdentity() === $username