2017-02-13 7 views
1

何らかの理由で、'service_manager'の設定が正しく読み取られていないと思われます。これは主に、まったく新しいスケルトンチェックアウトです。たぶん、日の仕事。ZF3にカスタマーサービスとサービスファクトリーが登録されていない

最近別の1つを実行して比較しようとしました。どこが間違っているのか分かりません。匿名関数の下で

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; 
    } 
} 
+0

よく見えます。私はコードを意味します。あなたは工場からDbBuilderControllerクラスを呼び出そうとしましたか? –

+0

ファクトリクラス自体の詳細を共有していないので、それ以上のことはできません。工場クラス '\ Application \ Service \ Factory \ UserServiceFactory :: class'は正しく参照されていますか?あなたが指し示しているフォルダや他の方法で誤って参照されたフォルダではなく、間違って名前を付けられている可能性があります... – Wilt

+0

間違って参照された場合は、エラーが発生します。私は意図的に誤って参照しようとしました。私はそれを含めるでしょう。 – Bluebaron

答えて

0

ZF3サービスマネージャを見ると、それはすなわちclass_exists('Application\Service\Factory\UserServiceFactory')が失敗して、ファクトリクラスをロードすることはできません示唆しています。

オートローダがApplication名前空間に登録され、いずれかModule.phpまたはcomposer.jsonに追加することにより、getAutoloaderConfig機能を使用していることを確認します。その後、(明らかにディレクトリを必要に応じて変更)

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

作曲家の更新を実行します。

もしZend\Loader\StandardAutoloaderを使用している場合は、UserServiceFactoryのディレクトリ構造にタイプミスがないかチェックしてください。

関連する問題