How to autoload classes without namespaces with Composer without reinstalling?
Asked Answered
J

3

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?

Jurist answered 28/7, 2013 at 17:4 Comment(0)
E
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';
// ...    
?>    
Earache answered 5/1, 2014 at 20:37 Comment(2)
I think a combination of giving composer file a name and running composer install composer update composer install composer update did it finallyHistoried
@Historied It did, at least even for meEberhard
H
15

Use classmap in instead of psr-4:

"autoload": {
    "classmap": ["models/"]
}
Hypogynous answered 26/3, 2018 at 6:35 Comment(0)
S
4

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

Skinnydip answered 2/8, 2013 at 15:19 Comment(1)
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.Cookgeneral

© 2022 - 2024 — McMap. All rights reserved.