PHP作曲家自动加载未加载类 [英] PHP composer autoload not loading class
问题描述
我刚刚开始使用composer进行依赖项管理,并且很难弄清楚如何不遵循psr-4进行自动加载,因此我在这里提供建议.我有一个生成随机值的类,该类已经存在于包装专家中.项目结构如下(我已将composer.json文件标记为A和B):
I'm just getting started using composer for dependency management and I'm having a hard time figuring out how I'm not adhering to psr-4 for autoloading so I'm here for advice. I've got a class that generates random values that is already on the packagist. The project structure is the following (I've labeled the composer.json files A and B):
项目目录
|classfile.php
A |composer.json
|vendor
|autoload.php
|ejfrancis
|php-random-value
B |composer.json
|RandomValue.php <--the class I want autoloaded
composer.json A
composer.json A
{
"require": {
"ejfrancis/php-random-value": "dev-master"
}
}
composer.json B
composer.json B
{
"name": "ejfrancis/php-random-value",
"description": "Secure random value generator.",
"require": {
"php": ">=5.3.0"
},
"license": "MIT",
"autoload": {
"psr-4": {
"ejfrancis\\" : ""
}
}
}
最后是RandomValue.php文件,该文件声明了ejfrancis命名空间
and finally the RandomValue.php file, which declares the ejfrancis namespace
namespace ejfrancis;
class RandomValue{
//foo
}
运行应用程序时,出现错误找不到类RandomValue",因此无法正确自动加载.我是否不遵守psr-4,还是我做错了其他事情?我还尝试仅使用"classmap" : ["RandomValue.php"]
之类的作曲家类映射进行自动加载,但未成功.
感谢您的帮助
When I run the app I get an error 'class RandomValue not found', so it's not autoloading correctly. Am I not complying to psr-4, or is there something else I'm doing wrong? I've also tried autoloading just using a composer classmap like "classmap" : ["RandomValue.php"]
to no success.
Thanks for the help
更新:我已经在composer.json B文件上运行了"composer validate",它肯定是有效的
Update: I've run 'composer validate' on the composer.json B file, it definitely is valid
推荐答案
将作曲家更改为"Namespace\\"
并执行作曲家dump-autoload -o
Change in your composer to "Namespace\\"
and do a composer dump-autoload -o
这篇关于PHP作曲家自动加载未加载类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!