I want my PHP classes to be loaded by Composer. It works when I follow the directory structure, according to the namespaces.

For example, my class \MyCompany\MyProject\Class1 is loaded when located in src/MyCompany/MyProject/Class1.php. I use this composer settings:

"autoload": {
    "psr-0": { "MyCompany\\MyProject\\": "src/" }
}

However, I do not want to put all my files to MyCompany directory since it is the only one in src. I know Java does it like this, but still. Is there any way to set up Composer so it will load my classes when they are directly in src? For example from file src/Class1.php.

Edit: PSR-4 addresses directly this problem, although classmap still works (and it's recommended for production setup).

有帮助吗?

解决方案

What about this?

"autoload": {
    "classmap": [
        "src"
    ]
}

其他提示

Another way is to use PSR-4, especially if you have more than a few classes i.e.

"autoload": {
    "psr-4": { "MyCompany\\MyProject\\": "src/" }
}

This should put you right where you want to be. But, as was in my own experience, this may require some tinkering till you get it right. See more here: http://seld.be/notes/psr-4-autoloading-support-in-composer

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top