20

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?

3 Answers 3

18

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';
// ...    
?>    
Sign up to request clarification or add additional context in comments.

2 Comments

I think a combination of giving composer file a name and running composer install composer update composer install composer update did it finally
@Toskan It did, at least even for me
16

Use classmap in instead of psr-4:

"autoload": {
    "classmap": ["models/"]
}

Comments

4

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

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.