出於某種原因,我不認爲我的'service_manager'
配置正在被正確讀取。這幾乎是一個全新的骷髏結帳。也許一天工作。客戶服務和服務工廠沒有註冊ZF3
我最近做了另一個,並試圖比較。我無法弄清楚我出錯的地方。
在匿名函數指向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;
}
}
好吧..看起來不錯。我的意思是代碼。你有沒有嘗試通過工廠調用DbBuilderController類? –
既然你沒有共享工廠類本身的任何細節,我不能再做更多的事情然後問:是否正確引用了工廠類'\ Application \ Service \ Factory \ UserServiceFactory :: class'?它可能被錯誤地命名,不在您指向的文件夾中或以其他方式被錯誤引用... – Wilt
如果引用不正確,則會引發錯誤。我甚至試圖故意不正確地引用它。我將包括它。 – Bluebaron