简体   繁体   中英

PSR4 not working

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": "git@bitbucket.org: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"
        }
    }
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM