2
votes

I have the following file structure:

  • ajon/
    • project/
      • src/
        • subproject1/
          • index.php
          • composer.json
        • subproject2/
          • composer.json

In my composer.json I have:

"psr-4": {
   "ajon\\project\\subproject1\\": "src/subproject1/",
   "ajon\\project\\subproject2\\": "src/subproject2/"
}

Within subproject1 I have Models/Foo.php which looks like:

namespace ajon\project\subproject1\Models;

class Foo { ... }

within subproject1/index.php I have:

require_once 'vendor/autoload.php';
use ajon\project\subproject1\Models\Foo;

I am getting:

Class 'ajon\project\subproject1\Models\Foo' not found.
  • Will composer look for the namespace ajon\project\subproject1 in the correct folder and then know to look in Models\Foo from there?

  • Am I doing something wrong?

  • Is there a better way to do this?
1

1 Answers

1
votes

I figured it out. It turns out the psr-4 directive is relative to where the composer.json file is.

I set composer.json as such:

"psr-4": { "ajon\\project\\subproject1\\": "./" }

and it worked perfectly.