0

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

我想实现:

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

我现在拥有的:2013年 4 月 10 日更新)

跟进下面的评论,我做了更多的研究,特别是:

我选择删除所有 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',
        ),
    ),
);

我希望这是正确的方法,而不是离正确的方法太远..

4

1 回答 1

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例外并更改了httpd.conf通过 WHM。

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

于 2013-10-05T16:34:55.380 回答