简体   繁体   中英

composer autoloader psr-0 namespaces

I have create a custom composer package but I am having troubles to set the correct autoload options for it.

All my classes are under MyNamespace/Common namespace. So for example for including my ArrayHelper class I do use Mynamespace/Common/Helper/ArrayHelper .

This is the relevant part of my composer.json :

"autoload": {
    "psr-0": { "MyNamespace\\": "" }
} 

I have read this: composer.json / autoload

Any help?

You have to navigate the file location of your namespace.

"autoload": {
    "psr-0": { "MyNameSpace": "./<path to your parent directory>" }
}

For example, this is my directory structure:

composer.json
source
  \-Data
    |-Controller
    \-Repository

Then, in the composer.json file:

"autoload": {
    "psr-0": { "MyNameSpace": "source/Data" }
}

Then, I can define classes in these namespaces:

/* namespace for classes in controller directory */
namespace MyNameSpace\Controller;

/* namespace for classes in repository directory */
namespace MyNameSpace\Repository;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM