15

I just need to autoload some classes, and I don't like the psr-0 namespace insanity (no offense).

This used to work just fine in my project:

"psr-0": {
    "": [
        "app/controller/",
        "app/model/"
    ]
}

For some reason it doesn't work anymore, even though I'm using the same Composer version. I need it for a new project that is also using Silex. Could this be a conflict with Silex?

I know about the "classmap" option, but it's kind of useless because it requires that I run "composer install" every time I add a new class.

Any ideas?

ChocoDeveloper
  • 12,930
  • 19
  • 74
  • 113

3 Answers3

15

Try to use "primitive" JSON properties; not an array (like in your example). This works for me with psr-4 like you say, with "": "app/":

{
"autoload": {
    "psr-4": {
        "Robbie\\": "core/",
        "": "app/"
    }
},
"require": {
        "monolog/monolog": "1.2.*"
    } 
}

This gives me the Robbie namespace under the directory core, as an example of sources not controlled by composer, the 3rd party (vendor) Monolog namespace and my default or non-namespace for sources underneath the app directory.

After a composer update, all of them are available when including the generated autoload.php:

<?php    
require_once 'vendor/autoload.php';
// ...    
?>    
raoulsson
  • 12,122
  • 11
  • 38
  • 61
  • 1
    I think a combination of giving composer file a name and running composer install composer update composer install composer update did it finally – Toskan Jan 13 '16 at 21:58
  • @Toskan It did, at least even for me – tonix Dec 30 '18 at 23:30
11

Use classmap in instead of psr-4:

"autoload": {
    "classmap": ["models/"]
}
qwertzguy
  • 10,735
  • 5
  • 53
  • 59
4

If you just want to regenerate the autoload file use composer dump-autoload.

cschorn
  • 1,095
  • 10
  • 11
  • Yes, this seemed to help also with the snipped of the asker's snippet. So seems like anyway must be explicitly defined, psr is not enabled by default ofc. – FantomX1 Jun 03 '20 at 17:05