Zend框架3中的身份验证服务

时间:2017-06-27 10:00:34

标签: zend-framework zend-auth zend-framework3

ZF3中的身份验证服务是否仍然可用?如果没有,新的名称是什么?它的工作原理是否相同?

我收到错误消息:

  

Class' Import \ AuthenticationService'找不到

我让作曲家抓住它:

composer

我也在zend-framework.com上搜索过它,我无法找到任何移植主题的信息。

因为我来自ZF1,我想问一下我的想法是否正确。我在Module.php中实现了authenticationService作为工厂:

'factories' => [

                    Model\Authentication::class => function ($container){
                        $auth = new AuthenticationService();
                        $dbAdapter = $container->get(AdapterInterface::class);  // AND aktiv != 0
                        $dbTableAuthAdapter  = new DbTableAuthAdapter($dbAdapter, 't_user','accessname','password', 'SHA2(?, 512)');
                        $auth->setAdapter($dbTableAuthAdapter);
                        return $auth;
                    },

我将auth服务连接到我的控制器(Module.php):

Controller\IndexController::class => function($container) {
                        return new Controller\IndexController(
                                $container->get(Model\UserTable::class),
                                $container->get(Model\Authentication::class),
                                $container->get(AdapterInterface::class)
                                );
                    },

我的Indexcontroller如下所示:

class IndexController extends AbstractActionController
{
    private $loginTable;
    private $authService;
    private $db;

public function __construct(UserTable $loginTable, AuthenticationService $authService, AdapterInterface $db)
{
    //$db=$this->db ;
    $this->loginTable = $loginTable;
    $this->authService  = $authService;
    $this->db=$db;
}

//Anzeigen der importierten Dateien
public function indexAction()
{
    $berechtigung = (int) $this->loginTable->getBerechtigung($this->authService->getIdentity());
    if(!$this->authService->hasIdentity() || $berechtigung > 1){ // 1 = Administrator
        return $this->redirect()->toRoute('user', ['action' => 'login']);
    }
    else {

        return $this->redirect()->toRoute('project', ['action' => 'index']);
    }

}

这是实施身份验证的正确方法吗?

1 个答案:

答案 0 :(得分:2)

要安装zend-authentication,请在终端中输入

new

关于你的错误信息

  

Class' Import \ AuthenticationService'找不到

您应该像完整名称空间一样使用它

composer require zendframework/zend-authentication