PSR4 not working

335 views Asked by At

I have setup a folder structure like this for a package of legacy classes

vendorname/legacy/src/ClassA.php

namespace Vendorname\Legacy;
class ClassA{}

vendorname/legacy/src/Folder/Class2.php
namespace Vendorname\Legacy\Folder;
class FolderClass2{}

With composer I'm loading this from a github repo like this:

"repositories": [
    {
        "type": "vcs",
        "url": "[email protected]:username/vendorname-legacy-classes.git"
    }
],
"require": {
    "vendorname/legacy": "master@dev"
}

When I load ClassA like this it works:

use Vendorname\Legacy\ClassA;

$a = new ClassA();

However none of my subfolder'd classes work:

use Vendorname\Legacy\Folder\FolderClassB;

$b = new FolderClassB();



Class 'Vendorname\\Legacy\\Folder\\FolderClassB' not found

I have already defined the source folder with a file vendor\vendorname\composer.json

{
    "name": "vendorname/legacy",
    "description": "Vendorname Legacy classes",
    "require": {
        "php": ">=5.3.0"
    },
    "autoload": {
        "psr-4": {
            "Vendorname\\Legacy\\": "src"
        }
    },
    "extra": {
        "branch-alias": {
            "master": "master"
        }
    }
}
1

There are 1 answers

2
Rômulo M. Farias On BEST ANSWER

you need to define one thing more to your composer.json

{
    "autoload": {
        "psr-4": {"Vendorname\\Legacy\\": "vendorname/legacy/src/"}
    }
}