Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composer autoload not loading class in Silex

I've got a situation using composer to load a class in a Silex app.

This class is located at:

src/custom/UserProvider.php

In my composer.json, I've added this lines:

"autoload": {
   "psr-0": {
        "CustomNamespace": "src/custom/"
    }
}

Inside my UserProvider.php file, I've got:

namespace CustomNamespace;

When I ran composer update in console, this line was added to the /vendor/composer/autoload_namespaces.php

'CustomNamespace' => $baseDir . '/src/custom/',

But, when I try to use the class:

new CustomNamespace\UserProvider();

I got this error:

Fatal error: Class 'CustomNamespace\UserProvider' not found in /home/ubuntu/www/project/web/index.php on line 27

Does anyone knows what's going on? Thanks!

like image 219
joaobarbosa Avatar asked Feb 18 '13 01:02

joaobarbosa


2 Answers

The problem is exactly what @Maerlyn said in his comment.

Everthing works fine when I moved my file to src/custom/CustomNamespace/UserProvider.php

like image 191
joaobarbosa Avatar answered Sep 23 '22 10:09

joaobarbosa


I'll just add a note here if someone comes across my issue, which displayed itself the same as this question, but had to do with case sensitivity.

I moved my site from OS X (not case sensitive) to Ubuntu (case sensitive), which complained that the class did not exist. The file was named myclass.php, and when I renamed it to MyClass.php it worked.

like image 42
Hans N. Hjort Avatar answered Sep 22 '22 10:09

Hans N. Hjort