我正在尝试创建我在 ZF2 中实现的服务实例。
模块.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\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 实例化服务
//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->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。有没有不同的方法可以做到这一点,因为我没有这个文件,也不知道它应该是什么样子?