ZF2 load service config from module

本小妞迷上赌 提交于 2019-12-02 09:25:21

I've finally found a solution. Jurian's hints to using the actual application have put me on the right track! :)

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

Everything is default, just make sure the module is added. I commented the 'application' module as I don't see any use for it (as of now). I also had to change the path to the config files from './module' to __DIR__ . '../module' as it was looking in the wrong directory (took me a while to find that one).

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

2 - configuration

make sure the modules are configured right, and also that ZF2 Path is set up correctly. In my case, run through the quick start on RTD (http://zf2.readthedocs.org/en/latest/ref/installation.html). I had the ZF2_PATH exception and change the httpd.conf via WHM.

3 - Read more on RTD

In particular on how you can bootstrap the application: http://zf2.readthedocs.org/en/latest/modules/zend.mvc.intro.html#zend-mvc-intro

Which after very little debugging produced me the following code to access a neatly configured $sm instance.

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

I do not like the fact that the configuration needs to be specified in addition to the init_autoloader path. I avoid this implementation from being copied and pasted all over the place, I am considering integrating the $sm instantiation into the init_autoloader.php in the future so that the path of the configuration file does not have to be specified whenever a ProductImage service needs to be invoked.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!