未找到Composer PSR-4 Autoload类

问题描述 投票:2回答:1

我一直在打我的头几个小时试图弄清楚为什么自动加载不适用于“Authentication \ auth()”。 “dBase \ db()”类加载得很好,但我得到:

错误:第8行的/var/htdocs/dev/test.php中找不到类'Authentication \ auth'

在调用test.php时。

file_structure

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文件中未指定自动加载以进行测试。

php namespaces composer-php autoload psr-4
1个回答
4
投票

这里的问题是,实际上你不使用PSR-4。在PSR-4中,类名应与文件名匹配。对于db类它很好,因为db类位于db.php文件中,但auth类位于authClass.php文件中,这就是问题所在。您应该将文件名更新为auth.php

您可能需要运行:

composer dump-autoload

另外在实际包中请记住,一个供应商包有一个名称空间,因此您不会为单个包创建多个名称空间,而只能创建单个名称空间

© www.soinside.com 2019 - 2024. All rights reserved.