出于某种原因,我认为我的'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;
}
}