Search code examples
phpzend-framework2packagecomposer-phpautoloader

How autoloader works in zend framework 2


I am just trying to create a package for packagist, a zend framework 2 user authentication module, https://packagist.org/packages/tahmina8765/zf2auth

When I keep this zf2auth folder in my modules folder, it works. But when I download it with composer, it downloaded in vendor/tahmina8765/zf2auth. In this folder, it does not work. I mean, I have added this module in application.config.php -

'modules' => array(
    ...
    'Zf2auth'
),

but here it does not work. If I keep it on step ahead, ie. vendor/zf2auth it works again.

How can I make it workable in vendor/tahmina8765/zf2auth folder?


Solution

  • I think there's a problem with the composer.json file in your module. It says:

    "autoload": {
        "psr-0": {
            "Zf2auth": "./"
        }
    }
    

    and looking at the directory structure it should be:

    "autoload": {
        "psr-0": {
            "Zf2auth\\": "src"
        }
    }
    

    It works in your ./modules/ dir because of the getAutoloaderConfig you have in your module class -- that's done by ZF2. When you download the module through composer however, the autoloading (probably) get's done by composer and the autoload (invalid) definition in your composer manifest is used. You might also want to run composers dump command afterwords, to refresh the autoloading classes.