php – Zend2 Doctrine2身份验证

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Zend2 Doctrine2身份验证脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我想使用Doctrine 2和Zf2进行身份验证.为了获得一些帮助,我使用了 Zend 2 + doctrine 2 Auth Adapter,但每次调用$authService-> authenticate($adapter);我得到一个错误,该类”不存在.

看来我的module.con@L_360_4@.PHP中的配置不会被使用.它显示如下:

'authenticationadapter' => array(
        'orm_default' => array(
            'objectManager' => 'doctrine.entITymanager.orm_default','identityClass' => 'PRofile\Entity\User','identityProPErty' => 'username','credentialProperty' => 'password',),

但是如果我在authService上创建了一个var_dump,那么所有的set都是null.
在我的服务中我想要登录打电话

$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显示如下:

public function getAuthAdapter()
{
    if (null === $this->authAdapter) {
        $this->authAdapter = $this->getServiceManager()
            ->get('doctrine.authenticationadapter.ormdefault');
    }
    return $this->authAdapter;
}

那么有人可以告诉我如何正确设置选项吗?

@H_777_26@ 看起来您使用的是错误的配置值.如果查看DoctrineoRM文档,它们将使用以下命令设置身份验证适配器的配置:
'doctrine' => array(
    'authentication' => array(
        'orm_default' => array(
            'object_manager' => 'Doctrine\ORM\EntityManager','identity_class' => 'Application\Entity\User','identity_property' => 'email','credential_property' => 'password',)

因此,而不是使用authenticationadapter在module.config.PHP中使用身份验证;而不是使用ObjectManager使用object_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']);

请关注documentation.

脚本宝典总结

以上是脚本宝典为你收集整理的php – Zend2 Doctrine2身份验证全部内容,希望文章能够帮你解决php – Zend2 Doctrine2身份验证所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。