不带Bootstrap.php的模块服务的ZF2依赖项注入

不带Bootstrap.php的模块服务的ZF2依赖项注入,php,dependency-injection,zend-framework2,Php,Dependency Injection,Zend Framework2,我正在尝试创建一个在ZF2中实现的服务实例 Module.php namespace ProductImage; use Zend\Db\ResultSet\ResultSet; use ProductImage\Model\ProductImageStorage; use ProductImage\Service\ProductImageService; use Zend\ModuleManager\Feature\ServiceProviderInterface; use Zend\Db\

我正在尝试创建一个在ZF2中实现的服务实例

Module.php

namespace ProductImage;

use Zend\Db\ResultSet\ResultSet; use ProductImage\Model\ProductImageStorage; use ProductImage\Service\ProductImageService; use Zend\ModuleManager\Feature\ServiceProviderInterface; use Zend\Db\TableGateway\TableGateway; use ProductImage\Model\ProductImageTable; use ProductImage\Model\ProductImage;

class Module implements ServiceProviderInterface {
    /* Invoked by Module Manager */
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__  => __DIR__ . '/src/' . __NAMESPACE__,
                    'ImageResizer' => __DIR__ . '/src/ImageResizer'
                ),
            ),
        );
    }

    /* Invoked by Module Manager */
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    /* Configure DB Service Managers */
    public function getServiceConfig()
    {
        return array(
            'factories'  => array(
                'ProductImage\Model\ProductImageTable'     => function($sm)
                {
                    $tableGateway = $sm->get('ProductImageTableGateway');
                    $table = new ProductImageTable($tableGateway);
                    return $table;
                },
                'ProductImageTableGateway'                 => function ($sm)
                {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new ProductImage());
                    return new TableGateway('F3G_IMAGES', $dbAdapter, null, $resultSetPrototype);
                }
            ),
            'invokables' => array(
                'productImageService'=> function ($sm)
                {
                    $productImageTable = $sm->get('ProductImageTable\Model\ProductImageTable');
                    return new ProductImageService($productImageTable);
                }
            ),
        );
    } }
//initialise a standard loader
set_include_path(get_include_path() . PATH_SEPARATOR . '/usr/lib/zendframework/library/');
require_once 'Zend/Loader/AutoloaderFactory.php';
use Zend\Loader\AutoloaderFactory;
Zend\Loader\AutoloaderFactory::factory(array(
        'Zend\Loader\StandardAutoloader' => array(
            'autoregister_zf' => TRUE,
        )
    )
);
$di = new Zend\Di\Di();
$piService = 
$di->get('ProductImage\Service\ProductImageService'); // (< throws Zend\\Di\\Exception\\ClassNotFoundException)
var_export($piService);
die();
ProductImage\Service\ProductImageService

class ProductImageService implements ServiceLocatorAwareInterface
{
    protected $productImageTable;
    protected $services;

    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->services = $serviceLocator;
    }

    public function getServiceLocator()
    {
        return $this->services;
    }

    function __construct(ProductImageTable $productImageTable)
    {
        $this->productImageTable = $productImageTable;
    }
    // ...
}
在独立php文件(functions.inc.php)中使用DI实例化服务


查看docs(),我的模块中似乎需要一个Bootstrap.php来配置DI。由于我没有此文件,也不知道它应该是什么样子,是否有其他方法来执行此操作?

除了您的原始问题,还有一个最佳做法建议:永远不要将ServiceLocator注入到您的服务中。只注入实际需要的依赖项(如TableGateway或EntityManager)。无法帮助解决您原来的问题tho:SThanks@Sam。这是试图让Zend服务管理器接收我的服务,显然您必须实现ServiceLocatorAwareInterface(请参阅)。我现在已经删除了这个依赖项,因为它在这一点上没有任何用途。谢谢@山姆你好,山姆!如果我的服务需要延迟加载,你会有什么建议?编写自己的组件或使用服务定位器?PS:我认为很多人低估了服务管理器。DI和服务定位器都有某些缺点。因为没有什么是完美的。我认为你需要使用的每一种工具都要考虑到它的特点。尽管创始人没有看到太大的不同:我找到了解决方案:
[Wed Oct 02 10:37:41 2013] [error] [client 192.168.6.52] PHP Fatal error:  Uncaught exception 'Zend\\Di\\Exception\\ClassNotFoundException' with message 'Class ProductImage\\Service\\ProductImageService could not be located in provided definitions.' in /usr/lib/zendframework/library/Zend/Di/Di.php:264\nStack trace:\n#0 /usr/lib/zendframework/library/Zend/Di/Di.php(227): Zend\\Di\\Di->newInstance('ProductImage\\Se...', Array, true)\n#1 /home/app/public_html/wp-content/themes/manage-product-images/functions.inc.php(34): Zend\\Di\\Di->get('ProductImage\\Se...')\n#2 /home/app/public_html/wp-content/themes/functions.inc.php(14): require_once('/home...')\n#3 /home/app/public_html/wp-content/themes/functions.php(14): require_once('/home...')\n#4 /home/app/public_html/wp-settings.php(293): include('/home...')\n#5 /home/app/public_html/wp-config.php(90): require_once('/home...')\n#6 /home/app/public_html/w in /usr/lib/zendframework/library/Zend/Di/Di.php on line 264