php 作曲家自动加载器 psr-0 命名空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18146057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
composer autoloader psr-0 namespaces
提问by brpaz
I have create a custom composer package but I am having troubles to set the correct autoload options for it.
我创建了一个自定义 Composer 包,但无法为其设置正确的自动加载选项。
All my classes are under MyNamespace/Common
namespace. So for example for including my ArrayHelper
class I do use Mynamespace/Common/Helper/ArrayHelper
.
我所有的类都在MyNamespace/Common
命名空间下。因此,例如为了包括我的ArrayHelper
课程,我确实使用Mynamespace/Common/Helper/ArrayHelper
.
This is the relevant part of my composer.json
:
这是我的相关部分composer.json
:
"autoload": {
"psr-0": { "MyNamespace\": "" }
}
I have read this: composer.json / autoload
我读过这个:composer.json / autoload
Any help?
有什么帮助吗?
回答by KarelG
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:
然后,在 composer.json 文件中:
"autoload": {
"psr-0": { "MyNameSpace": "source/Data" }
}
Then, I can call the namespace from PHP by
然后,我可以从 PHP 调用命名空间
/* namespace for classes in controller directory */
namespace MyNameSpace\Controller
/* namespace for classes in repository directory */
namespace MyNameSpace\Repository