7

Currently all my classes are in one folder and are under one namespace:

"psr-4": {
   "RootNamespace\\": "lib/"
},

This is working well. As I'm adding more and more classes, I would like to put some logically related classes into deeper namespace level, but with the same root namespace. It should be something like this:

RootNamespace/Services (in 'lib/services' dir)
RootNamespace/Listeners (in 'lib/listeners' dir)

I suppose so that I don't need to change anything in my composer.json ps-4 autoload definition, but it's not working anymore.

How autoload definition should look like to achieve what I want to?

As I tested, solution below is not a good since, declarations seems to be overwritten

"psr-4": {
   "RootNamespace\\": "lib/",
   "RootNamespace\\Services\\": "lib/services/",
   "RootNamespace\\Listeners\\": "lib/listeners/"
},
kkochanski
  • 2,178
  • 23
  • 28
  • You shouldn't need to do this, you just need to change the case on your folders (ie `lib/services` -> `lib/Services` as your name space has an uppercase `S` and remove the extra stuff in `psr-4` just keep the main `"RootNamespace\\": "lib/",` – cmorrissey Jan 19 '17 at 21:55
  • It's working when I run `composer dump-autoload`, but then vendor classes are not found. So I'm just a little stuck and have no idea what to do with it :P – kkochanski Jan 19 '17 at 22:08

1 Answers1

2

According to the PSR-4 Spec:

All class names MUST be referenced in a case-sensitive fashion.

Your configuration is accurate having different PSR-4 namespaces nested in the same directory. It should work, but may become confusing down the road.

"psr-4": {
    "RootNamespace\\": "lib/",
    "RootNamespace\\Services\\": "lib/services/",
    "RootNamespace\\Listeners\\": "lib/listeners/"
},

I recommend you either simply capitalize your directories to match the PSR spec, or move your RootNamespace out of the top level lib/ directory.

Steve Buzonas
  • 5,300
  • 1
  • 33
  • 55