5

I'm beginning to study Composer and am developing a system where I separate the files core application files, as follows:

/root 
    |-- /src 
         |-- /App 
               |-- /DBConfig
               |-- /Controller
               |-- /Model
         |-- /Core 
               |-- /Helper
               |-- /Controller
               |-- /Model

So, to set this setting in composer.json file and get access to all classes both /App much /Core would be this way?


    "autoload" : {
        "psr-X" : {
            "App\\" : "/src",
            "Core\\" : "/src"
        }
    }

Or is there a more correct way?

I have also read about PSR-0 vs PSR-4 and I am still somewhat in doubt which one to use. In my case what should I implement, PSR-0 or PSR-4?

LeoFelipe
  • 281
  • 6
  • 13

1 Answers1

5

You didn't need 2 entries just one for the main namespace so something like this for PSR-4:

    "autoload" : {
        "psr-4" : {
            "MyApp\\" : "/src"            }
    }

As long as everything in src/ uses the same namespace that's all you'll need. Just let the autoloader do it's job.

As to which to use I'd go with PSR-4 because at some point it is expected that PSR-0 will be deprecated and as PSR-4 is made to be backwards compatible minus some warts for older legacy programs there isn't really a difference except of you start using some of it newer features

Dragonaire
  • 313
  • 3
  • 7
  • But in this case, I shall create an intermediate directory, for example: /src/Project/App/Controller/ . Is it? – LeoFelipe Jun 04 '14 at 22:59
  • no src/ = MyApp\ in PSR-4 namespaces. Think of the first namespace part as an alias to save using the whole path in this case. That not what it's really for but it does have that effect in PSR-4. So your path would be /src/App/Controller and in the file you'd have namespace MyApp\App\Controller; – Dragonaire Jun 06 '14 at 03:28