GetServiceLocator in Zend Framework 3

一个人想着一个人 提交于 2019-12-02 09:19:13

There is no more service locator in ZF3 as it is considered as an antipattern.

The proper way to proceed is to use factories in the service manager, and pass specific dependencies into your class.

If you have any code you can show, I'll be happy to help you further.


Edit according to the example provided.

First thing first, use composer for autoloading rather than the old Zend stuff. In Module.php, remove the autoloading. Removing the autoload_classmap file is also required.

Add a composer.json file and set your PSR-0 or PSR-4 autoloading (ask if you don't know how).

Back on the Module class, you also need to change the service manager configuration. I'm keeping your anonymous functions here but you should use proper classes.

<?php

namespace SanAuth;

use Zend\Authentication\Storage;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;

final class Module
{
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getServiceConfig()
    {
        return [
            'factories'=> [
                \SanAuth\Model\MyAuthStorage::class => function($container){
                    return new \SanAuth\Model\MyAuthStorage('zf_tutorial');
                },
                'AuthService' => function($container) {
                    $dbAdapter = $sm->get(\Zend\Db\Adapter\Adapter::class);
                    $dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'users','user_name','pass_word', 'MD5(?)');
                    $authService = new AuthenticationService();
                    $authService->setAdapter($dbTableAuthAdapter);
                    $authService->setStorage($container->get(SanAuth\Model\MyAuthStorage::class));

                    return $authService;
                },
            ),
        );
    }
}

Also, please consider using password_* functions rather than MD5 (or whatever, but not md5 anyways).

Let's focus on just one simple controller. Same things needs to be repeated for the others.

<?php

namespace SanAuth\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Authentication\AuthenticationService;

final class SuccessController extends AbstractActionController
{
    private $authenticationService;

    public function __construct(AuthenticationService $authenticationService)
    {
        $this->authenticationService = $authenticationService;
    }

    public function indexAction()
    {
        if (! $this->authenticationService->hasIdentity()){
            return $this->redirect()->toRoute('login');
        }

        return new ViewModel();
    }
}

Obviously you need to update the factory: https://github.com/samsonasik/SanAuth/blob/master/config/module.config.php#L10 (inject the service as parameter).

Looking at the code on github, the master branch introduces ZF3 compatibility from what I can see, so have a look there!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!