Php 为DoctrineModule';对象存储库

Php 为DoctrineModule';对象存储库,php,doctrine-orm,zend-framework2,Php,Doctrine Orm,Zend Framework2,我正试图通过Zend2的源代码找到我的路径,因为对于初学者来说,全面的文档几乎是不存在的 在那里,我找到了一个自定义身份验证适配器。此类接受的对象为。我只需要将credentialCallable值设置为基于自定义的函数 我为控制器编写了一个类来包装适配器: namespace User\Controller\Plugin; class UserAuthentication extends AbstractPlugin { protected $_authAdapter = null;

我正试图通过Zend2的源代码找到我的路径,因为对于初学者来说,全面的文档几乎是不存在的

在那里,我找到了一个自定义身份验证适配器。此类接受的对象为。我只需要将
credentialCallable
值设置为基于自定义的函数

我为控制器编写了一个类来包装适配器:

namespace User\Controller\Plugin;

class UserAuthentication extends AbstractPlugin {
    protected $_authAdapter = null;
    protected $_authService = null;

    public function __construct($authAdapter) {
        $this->setAuthAdapter($authAdapter);
    }
    // More setters/getters
}
现在,我需要以这样的方式配置模块:这里的调用将为我提供一个有效的实例

$uAuth = $this->getServiceLocator()->get('User\Controller\Plugin\UserAuthentication');
因此,很自然,我将不得不使用模块条件配置。但在这里,我完全被卡住了,因为我找不到任何关于如何正确创建类实例的提示。这就是我到目前为止的想法:

return array(
    'di' => array(
        'instance' => array(
            'User\Event\Authentication' => array(
                'parameters' => array(
                    'userAuthenticationPlugin' => 'User\Controller\Plugin\UserAuthentication',
                ),
            ),
            'User\Controller\Plugin\UserAuthentication' => array(
                'parameters' => array(
                    'authAdapter' => 'DoctrineModule\Authentication\Adapter\ObjectRepository'
                ),
            ),
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'DoctrineModule\Authentication\Adapter\ObjectRepository' => function ($sm) {
                /// ????
            },
            'DoctrineModule\Options\Authentication' => function($sm) {                
                /// ????
            },
        ),
    ),
);
所以我不知道该填什么,或者这是否是正确的方法。也许我完全走错了路,因为在执行此操作时,我得到:

An abstract factory could not create an instance of usercontrollerpluginuserauthentication(alias: User\Controller\Plugin\UserAuthentication).

我很感激你的建议和暗示。请不要告诉我ZfcUser或类似的东西,我想/需要自己实现。

我还没有在ZF2中使用过
Di
。但以下是我如何在ZF2中使用DoctrineModule
ObjectRepository

在我的
Module.php
中,我有一个用于AuthenticationService的工厂,就像您有一个AuthenticationService一样。在工厂中,我创建了一个新的
ObjectRepository
,其中包含它所需的所有值

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'AuthService' => function($services) {
                $entityManager = $services->get('doctrine.entitymanager.orm_default');
                $doctrineAuthAdapter = new ObjectRepository(array(
                    'objectManager' => $entityManager,
                    'identityClass' => 'Auth\Entity\User',
                    'identityProperty' => 'username',
                    'credentialProperty' => 'password',
                    'credentialCallable' => function($identity, $credential) {
                        return md5($identity->salt . $credential);
                    }, // this function makes the password hash salted
                       // you could also just use return md5($credential);
                ));

                // my AuthenticationService uses the entity manager
                // and the ObjectRepository
                $authService = new AuthenticationService();
                $authService->setEntityManager($entityManager);
                $authService->setAdapter($doctrineAuthAdapter);

                return $authService;
            },
        ),
    );
}
AuthenticationService
基本上是Zend的AuthenticationService的一个扩展,带有一些额外的方法,我发现它们很有用(而且
Zfc
使用,因为我从那里窥视)。为了简洁起见,我删除了实现,但保留了声明,以便让您看到我认为在服务中有用的内容

<?php

namespace Auth\Service;

use Application\EntityManagerAwareInterface;
use Zend\Authentication\AuthenticationService as ZendAuthenticationService;

use Auth\Entity\User;

class AuthenticationService extends ZendAuthenticationService implements EntityManagerAwareInterface
{
    /**
     * This method makes sure that we always get a User-object
     * we can call methods on. In case of a non-authorized user
     * we will receive a dummy object without storage or with
     * session storage. So data might be lost!
     */
    public function getIdentity()
    {
        $storage = $this->getStorage();

        if ($storage->isEmpty()) {
            return new \Auth\Entity\User\Dummy();
        }

        $userid = $storage->read();

        $user = $this->getEntityManager()->find('Auth\Entity\User', $userid);

        if ($user == null) {
            return new \Auth\Entity\User\Dummy();
        } else {
            return $user;
        }
    }

    /**
     * Register a new user to the system. The user password will by hashed before
     * it will be saved to the database.
     */
    public function register(User $user)
    {

    }

    /**
     * Reset the users password to a random value and send an e-mail to the
     * user containing the new password.
     */
    public function resetPassword(User $user)
    {

    }

    /**
     * Delete a users account from the database. This does not really delete the
     * user, as there are too many connections to all other tables, but rather
     * deletes all personal information from the user records.
     */
    public function delete(User $user)
    {

    }

    public function setEntityManager(\Doctrine\ORM\EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function getEntityManager()
    {
        return $this->entityManager;
    }
}

这很有帮助,谢谢。我马上就去试试。我还看到我的插件实际上是一个AuthService,我应该使用它。这个答案很有效,但有点笨拙。请看一个更简单的方法。
// getAuthService() just calls $this->getServiceLocator()->get('AuthService')

$this->getAuthService()->getAdapter()
     ->setIdentityValue($username)
     ->setCredentialValue($password);

$result = $this->getAuthService()->authenticate();

foreach($result->getMessages() as $message) {
    $this->flashmessenger()->addMessage($message);
}

if ($result->isValid()) {
     $this->getAuthService()->getStorage()->write($result->getIdentity()->getId());
     return true;
 } else {
     return false;
 }