没有Bootstrap.php的ZF2依赖注入模块服务

时间:2013-10-02 09:43:35

标签: 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\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);
                }
            ),
        );
    } }

ProductImage \服务\ 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文件中使用DI实例化服务(functions.inc.php)

//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();

$ di-&gt; get:

引发的错误
[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

查看文档(http://framework.zend.com/manual/2.1/en/tutorials/quickstart.di.html),我的模块中似乎需要一个Bootstrap.php来配置DI。有没有不同的方法这样做,因为我没有这个文件,不知道它应该是什么样的?

0 个答案:

没有答案