[英]Composer PSR-4 Autoload class not found
我一直在打我的头几个小时试图弄清楚为什么自动加载不适用于“Authentication \\ auth()”。 “dBase \\ db()”类加载得很好,但我得到:
错误:第8行的/var/htdocs/dev/test.php中找不到类'Authentication \\ auth'
在调用test.php时。
Root composer.json -
"require": {
"geeshoe/dbClass": "dev-develop",
"geeshoe/authClass": "dev-master"
},
"autoload": {
"psr-4": {
"dBase\\": "vendor/geeshoe/dbclass/",
"Authentication\\": "vendor/geeshoe/authClass/"
}
}
authClass.php标题 -
<?php
namespace Authentication;
use dBase\db;
class auth extends db
{
test.php -
if (file_exists("vendor/autoload.php")) {
require "vendor/autoload.php";
} else {
echo "Dam.. Something went wrong!";
}
$test = new \dBase\db();
$var = new \Authentication\auth();
如果有人可以向我指出显而易见的事情,那就太好了。 另外,在authClass-> composer.json文件中未指定自动加载以进行测试。
这里的问题是,实际上你不使用PSR-4。 在PSR-4中,类名应与文件名匹配。 对于db
类,它很好,因为db类位于db.php
文件中,但auth
类位于authClass.php
文件中,这就是问题所在。 您应该将文件名更新为auth.php
您可能需要运行:
composer dump-autoload
另外在实际包中请记住,一个供应商包有一个名称空间,因此您不会为单个包创建多个名称空间,而只能创建单个名称空间
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.