ZF2从模块加载服务配置

时间:2013-10-03 08:25:28

标签: php dependency-injection zend-framework2 servicemanager module-management

我仍在努力从Zend Framework之外的ZF2模块实例化一个服务(在一个空白的.php中)。

我想实现:

通过使用ServiceManager和可能的DI实例化+从ZF外部调用ZF2服务方法。

我现在拥有的内容:更新日期4/10/2013

根据以下评论,我做了更多的研究,特别是:

我选择删除所有DI和ModuleManager的东西并尝试自动加载(现在正常工作)并实例化(不)服务。

1 - 使用Classmap自动加载请求的类并在独立的.PHP文件中实例化servicemanager

// Autoload ZF and ProductImage module via classmap
Zend\Loader\AutoloaderFactory::factory(array(
        'Zend\Loader\StandardAutoloader'   => array(
            'autoregister_zf' => TRUE,
        ),
        'Zend\Loader\ClassMapAutoloader'   => array(
            '/home/frequency/domains/scrftcdn/public_html/ft/shop/php/zendframework/module/ProductImage/autoload_classmap.php',
        )
    )
)

// Hard-coded servicemanager configuration (will come from $module->getConfig once this works)
$smc = new \Zend\ServiceManager\Config(
    array(
        'service_manager' => array(
            'factories'       => array(
                'ProductImage\Model\ProductImage'   => 'ProductImage\Factory\ProductImageFactory',

            )
        ),
    )
);

// Instantiate the service manager
$sm = new \Zend\ServiceManager\ServiceManager($smc);

//Load the service via the service manager
$service = $sm->get('ProductImage\Model\ProductImage'); // <throws exception
die();

2 - 例外

 [error] [client 192.168.6.52] PHP Fatal error: 
 Uncaught exception 'Zend\\ServiceManager\\Exception\\ServiceNotFoundException' with message 'Zend\\ServiceManager\\ServiceManager::get was unable to fetch or create an instance for ProductImage\\Model\\ProductImage' in /usr/lib/zendframework/library/Zend/ServiceManager/ServiceManager.php:495
Stack trace:\n#0 /home/frequency/domains/wpfreqad/public_html/wp-content/themes/frequency/manage-product-images/functions.inc.php(48): Zend\\ServiceManager\\ServiceManager->get('ProductImage\\Mo...')
    #1 /home/frequency/domains/wpfreqad/public_html/wp-content/themes/frequency/functions.inc.php(14): require_once('/home/frequency...')\n
    #2 /home/frequency/domains/wpfreqad/public_html/wp-content/themes/frequency/functions.php(14): require_once('/home/frequency...')\n
    #3 /home/frequency/domains/wpfreqad/public_html/wp-settings.php(293): include('/home/frequency...')\n
    #4 /home/frequency/domains/wpfreqad/public_html/wp-config.php(90): require_once('/home/frequency...')\n
    #5 /home/frequency/domains/wpfreqad/public_html/wp-load.php(29): require_onc in /usr/lib/zendframework/library/Zend/ServiceManager/ServiceManager.php on line 495

3 - ProductImage \ autoload_classmap.php

    <?php
    // Generated by ZF2's ./bin/classmap_generator.php
    return array(
        'ProductImageTest\Service\ProductImageServiceTest'         => __DIR__ . '/test/ProductImageTest/Service/ProductImageServiceTest.php',
        'ProductImage\Module'    => __DIR__ . '/Module.php',
        'ProductImage\Factory\ProductImageFactory'                => __DIR__ . '/src/ProductImage/Factory/ProductImageFactory.php',
        'ProductImage\Model\ProductImage'                          => __DIR__ . '/src/ProductImage/Model/ProductImage.php',

    );

4 - ProductImage \ Module.php

class Module implements \Zend\ModuleManager\Feature\ConfigProviderInterface
{
    /* Invoked by Module Manager */
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }
}

5 - ProductImage \ config \ module.config.php

<?php
return array(
    'service_manager' => array(
        'factories'  => array(
            'ProductImage\Model\ProductImage'           =>  'ProductImage\Factory\ProductImageFactory',
        ),
    ),
);

我希望这是正确的方法,并且离正确的方向不太远。

1 个答案:

答案 0 :(得分:2)

我终于找到了解决方案。 Jurian使用实际应用程序的提示使我走上正轨! :)

1 - /zendframework/config/application.config.php。

一切都是默认的,只需确保添加模块即可。我对“应用程序”模块进行了评论,因为我认为它没有任何用处(截至目前)。我还必须将配置文件的路径从'./module'更改为__DIR__ . '../module',因为它查找的是错误的目录(花了一些时间才找到它)。

<?php
return array(
   // ...
 'modules' => array(
        'ProductImage', /* ProductImage module */
//        'Application',
    ),
// ...
'module_listener_options' => array(
    'module_paths' => array(
        __DIR__ . '/../module',
        __DIR__ . '/../vendor',
    ),

2 - 配置

确保模块配置正确,并确保ZF2路径设置正确。就我而言,在RTD上快速启动(http://zf2.readthedocs.org/en/latest/ref/installation.html)。我有ZF2_PATH例外,并通过WHM更改httpd.conf

3 - 详细了解RTD

特别是关于如何引导应用程序:http://zf2.readthedocs.org/en/latest/modules/zend.mvc.intro.html#zend-mvc-intro

经过极少的调试后,我生成了以下代码来访问整齐配置的$ sm实例。

//wherever the ZF2 application skeleton is, include the autoloader
require_once '/home/path/to/the/ZF2/application/directory/init_autoloader.php';

use Zend\Loader\AutoloaderFactory;
use Zend\Mvc\Application;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;

// setup autoloader
AutoloaderFactory::factory();

// get application stack configuration
$configuration = include '/home/path/to/the/ZF2/application/directory/config/application.config.php';

//var_export($configuration);
// The init() method does something very similar with the previous example.
$app = Application::init($configuration);
$sm = $app->getServiceManager();
$pi =  $sm->get('ProductImage\Service\ProductImageService');
var_export($pi);
die();

我不喜欢除了init_autoloader路径之外还需要指定配置的事实。我避免将此实现复制并粘贴到所有地方,我正在考虑将$sm实例化集成到init_autoloader.php中,以便不必每次都指定配置文件的路径需要调用ProductImage服务。