首页 文章

客户服务和服务工厂未在ZF3注册

提问于
浏览
1

出于某种原因,我认为我的 'service_manager' 配置没有被正确读取 . 这主要是一个全新的骨架结账 . 也许一天工作 .

我最近做了另一个,并尝试比较 . 我无法弄清楚我哪里出错了 .

Controller\DbBuilderController::class => function(ContainerInterface $container, $requestedName) { 指向的匿名函数下,行 $userService = $container->get(\Application\Service\UserService::class); 导致错误: Unable to resolve service "Application\Service\UserService" to a factory; are you certain you provided it during configuration?

我已经尝试将 \Application\Service\UserService::class 更改为简短,愚蠢的文字字符串,因此我确信该服务未被注册 .

我不确定为什么会这样 . 任何接受者?

<?php
namespace Application;

use Zend\Mvc\Application;
use Zend\Mvc\Controller\LazyControllerAbstractFactory;
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;
use Interop\Container\ContainerInterface;

use Doctrine\ORM\Mapping\Driver\AnnotationDriver;

return [
    'service_manager' => [
        'factories' => [
            \Application\Service\UserService::class => \Application\Service\Factory\UserServiceFactory::class
        ],
    ],
    'doctrine' => [
        'driver' => [
            __NAMESPACE__ . '_driver' => [
                'class' => AnnotationDriver::class,
                'cache' => 'array',
                'paths' => [__DIR__ . '/../src/Entity']
            ],
            'orm_default' => [
                'drivers' => [
                    __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
                ]
            ]
        ]
    ],

    'router' => [
        'routes' => [
            'home' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'createUser' => [
                'type' => Segment::class,
                'options' => [
                    'route'    => '/createuser/:username/:password',
                    'defaults' => [
                        'controller' => Controller\DbBuilderController::class,
                        'action'     => 'createUser',
                        'username' => '',
                        'password' => ''
                    ],
                ],
            ],
            'importTrades' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/importTrades',
                    'defaults' => [
                        'controller' => Controller\DbBuilderController::class,
                        'action'     => 'importTrades',
                    ],
                ],
            ],
            'createExchanges' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/createExchanges',
                    'defaults' => [
                        'controller' => Controller\DbBuilderController::class,
                        'action'     => 'createExchanges',
                    ],
                ],
            ],

        ],
    ],
    'controllers' => [
        'factories' => [
            Controller\IndexController::class => InvokableFactory::class,
            Controller\DbBuilderController::class => function(ContainerInterface $container, $requestedName) {
                $entityManager = $container->get('doctrine.entitymanager.orm_default');
                $userService = $container->get(\Application\Service\UserService::class);
                return new Controller\DbBuilderController($entityManager, $userService);
            },
        ],

    ],

    'view_manager' => [
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => [
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ],
        'template_path_stack' => [
            __DIR__ . '/../view',
        ],
    ],
    'strategies' => array(
        'ViewJsonStrategy',
    ),
];

工厂:

<?php

namespace Application\Service\Factory;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;

class UserServiceFactory implements FactoryInterface {

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $entityManager = $container->get('doctrine.entitymanager.orm_default');

        $user = new \Application\Service\UserService($entityManager);
        return $user;
    }

}

服务:

<?php

namespace Application\Service;

class UserService
{
    protected $entityManager;

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

    public function createUser($username, $password)
    {
        $user = new \Application\Entity\User();
        $user->setUserKey($password);
        $user->setUserName($username);
        $user->setIsAdmin(true);

        $this->entityManager->persist($user);
        $this->entityManager->flush();

        return $user;
    }
}

1 回答

  • 0

    查看ZF3服务管理器表明它无法加载工厂类,即 class_exists('Application\Service\Factory\UserServiceFactory') 失败 .

    确保自动加载器已为 Application 命名空间注册,使用Module.php中的 getAutoloaderConfig 函数或添加到composer.json中:

    "autoload": {
        "psr-4": {
            "Application\\": "module/Application/src/"
        }
    },
    

    (显然,根据需要更改了目录)然后运行composer update .

    如果您正在使用 Zend\Loader\StandardAutoloader ,也可以检查包含 UserServiceFactory 的目录结构是否存在拼写错误 .

相关问题