Php Zend2&x2B;Doctrine2身份验证
我希望使用条令2和ZF2进行身份验证。为了获得一些帮助,我使用了,但每次我调用Php Zend2&x2B;Doctrine2身份验证,php,authentication,doctrine-orm,zend-framework2,Php,Authentication,Doctrine Orm,Zend Framework2,我希望使用条令2和ZF2进行身份验证。为了获得一些帮助,我使用了,但每次我调用$authService->authenticate($adapter)我得到一个错误,类“”不存在 我的module.config.php中的配置似乎不会被使用。它显示如下: 'authenticationadapter' => array( 'orm_default' => array( 'objectManager' => 'doctrine.entity
$authService->authenticate($adapter)代码>我得到一个错误,类“”不存在
我的module.config.php中的配置似乎不会被使用。它显示如下:
'authenticationadapter' => array(
'orm_default' => array(
'objectManager' => 'doctrine.entitymanager.orm_default',
'identityClass' => 'Profile\Entity\User',
'identityProperty' => 'username',
'credentialProperty' => 'password',
),
),
public function getAuthAdapter()
{
if (null === $this->authAdapter) {
$this->authAdapter = $this->getServiceManager()
->get('doctrine.authenticationadapter.ormdefault');
}
return $this->authAdapter;
}
但是如果我在authService上进行了var_转储,那么所有的集合都是空的。
在我的服务中,我想登录我调用的
$authAdapter = $this->getAuthAdapter();
$authAdapter->setIdentityValue($username);
$authAdapter->setCredentialValue($password);
来自$authAdapter的转储告诉我IdentityValue和CredentialValue
设置正确
$authAdabter中的其他内容包括:
protected 'options' =>
object(DoctrineModule\Options\Authentication)[281]
protected 'objectManager' =>
object(Doctrine\ORM\EntityManager)[248]
private 'config' =>
object(Doctrine\ORM\Configuration)[250]
...
private 'conn' =>
object(Doctrine\DBAL\Connection)[252]
...
private 'metadataFactory' =>
object(Doctrine\ORM\Mapping\ClassMetadataFactory)[266]
...
private 'repositories' =>
array (size=0)
...
private 'unitOfWork' =>
object(Doctrine\ORM\UnitOfWork)[267]
...
private 'eventManager' =>
object(Doctrine\Common\EventManager)[242]
...
private 'hydrators' =>
array (size=0)
...
private 'proxyFactory' =>
object(Doctrine\ORM\Proxy\ProxyFactory)[268]
...
private 'expressionBuilder' => null
private 'closed' => boolean false
private 'filterCollection' => null
protected 'objectRepository' => null
protected 'identityClass' => null
protected 'identityProperty' => null
protected 'credentialProperty' => null
protected 'credentialCallable' => null
protected 'classMetadata' => null
protected 'storage' => null
protected '__strictMode__' => boolean true
protected 'authenticationResultInfo' => null
getAuthAdapter如下所示:
'authenticationadapter' => array(
'orm_default' => array(
'objectManager' => 'doctrine.entitymanager.orm_default',
'identityClass' => 'Profile\Entity\User',
'identityProperty' => 'username',
'credentialProperty' => 'password',
),
),
public function getAuthAdapter()
{
if (null === $this->authAdapter) {
$this->authAdapter = $this->getServiceManager()
->get('doctrine.authenticationadapter.ormdefault');
}
return $this->authAdapter;
}
有人能告诉我如何正确设置选项吗?看起来您使用了错误的配置值。如果查看DoctrineORM文档,他们将使用以下内容设置身份验证适配器的配置:
'doctrine' => array(
'authentication' => array(
'orm_default' => array(
'object_manager' => 'Doctrine\ORM\EntityManager',
'identity_class' => 'Application\Entity\User',
'identity_property' => 'email',
'credential_property' => 'password',
),
),
)
因此,不要在module.config.php中使用authenticationadapter
而使用authentication
;不要使用objectManager
而是使用object\u manager
等
在Module.php中,您需要添加以下内容:
use Zend\Authentication\AuthenticationService;
...
public function getServiceConfig()
{
return array(
'factories' => array(
'Zend\Authentication\AuthenticationService' => function($serviceManager) {
return $serviceManager->get('doctrine.authenticationservice.orm_default');
}
)
);
}
在控制器中,您可以使用以下方式访问适配器:
$authService = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService');
$adapter = $authService->getAdapter();
$adapter->setIdentityValue($data['login']);
$adapter->setCredentialValue($data['password']);
如果按照Hohner的建议在Module.php中使用“Zend\Authentication\AuthenticationService”,请遵循。,这将不适用于BjyAuthorize模块角色和ACL。BjyAuthorize将默认为使用“ZfcUser\Authentication\Storage\Db”的身份验证服务的默认配置。要让BjyAuthorize使用条令标识,请将其替换为(或添加)“zfcuser_auth_service”,如下所示:
'zfcuser_auth_service' => function ($serviceManager) {
return $authenticationService = $serviceManager->get('doctrine.authenticationservice.orm_default');
},
然后,您还可以在控制器中以类似的方式使用它:
$authService = $this->getServiceLocator()->get( 'zfcuser_auth_service' );
好的,我更改了它,但现在它告诉我Zend\ServiceManager\ServiceManager::get无法获取或创建doctor.authentication.orm\u default
的实例。我将getter更改为公共函数getAuthAdapter(){if(null====$this->authAdapter){$this->authAdapter=$this->getServiceManager()->get('doctrine.authentication.orm_default');}返回$this->authAdapter;
感谢您的帮助。现在只剩下一个问题了。$authResult=$authService->authenticate()之后
我得到一个空页面。没有错误,什么都没有。在您的实体中,您是否实现了任何接口或扩展了任何基类?不仅是使用vendor/bin/doctor模块orm:convert-mapping生成的实体——从数据库注释模块/Profile/src/--namespace=“Profile\\Entity\\”
还有setter和getter。好吧,我不知道是什么错误,但现在它可以工作了。谢谢你的帮助,请原谅我的英语不是我的母语。