Search code examples
phpzend-frameworkzend-framework2service-locator

loading classes from main vendor directory from a zf2 module


I'm writing a PHP application with Zend Framework 2.3.1.

I wrote a ZF2 module that I placed in vendor directory. vendor/TuxDrink.

That module requires another module that I placed in vendor/ServiceLocatorFactory (https://github.com/fezfez/ServiceLocatorFactory).

now.. I want to use the ServiceLocatorFactory Module inside my module. but that ServiceLocatorFactory directory is in the vendor directory of the application, not of my specific module.

my TuxDrink Module.php

<?php

namespace TuxDrink;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;

class Module implements AutoloaderProviderInterface
{
public function getAutoloaderConfig()
{
    return array(
        'Zend\Loader\ClassMapAutoloader' => array(
            __DIR__ . '/autoload_classmap.php',
        ),
        'Zend\Loader\StandardAutoloader' => array(
            'namespaces' => array(
                __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
            ),
        ),
    );
}
}

what exactly do I need to add in order for my TuxDrink module to detect the ServerLocatorFactory module and allow me to use it's classes.

for now I get

Fatal error: Class 'ServiceLocatorFactory' not found

any information regarding the issue would be greatly appreciated.


Solution

  • A couple of things/best-practices to consider:

    1) If it's vendor/, it gets put there by composer. That way, all the autoloading is configured properly. Also, .gitignore typically ignores everything in vendor/, so keep that in mind.

    2) That 'ServiceLocatorFactory' module is a very bad idea. The best practice is properly inject real dependencies - . A module whose purpose is to expose the ServiceManager globally is an anti-pattern. You'll never know what dependencies your classes have, because any of them can pull any dependency from anywhere.