简体   繁体   English

未找到Composer PSR-4 Autoload类

[英]Composer PSR-4 Autoload class not found

I have been beating my head for a couple hours trying to figure out why the autoload is not working for "Authentication\\auth()". 我一直在打我的头几个小时试图弄清楚为什么自动加载不适用于“Authentication \\ auth()”。 The "dBase\\db()" class is loading just fine, but i'm getting: “dBase \\ db()”类加载得很好,但我得到:

Error: Class 'Authentication\\auth' not found in /var/htdocs/dev/test.php on line 8 错误:第8行的/var/htdocs/dev/test.php中找不到类'Authentication \\ auth'

when calling test.php. 在调用test.php时。

file_structure

Root composer.json - 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 header - authClass.php标题 -

<?php
namespace Authentication;

use dBase\db;

class auth extends db
{

test.php - 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();

If someone could point out the obvious to me, that would be great. 如果有人可以向我指出显而易见的事情,那就太好了。 On a side note, autoload is not specified in the authClass->composer.json file for testing purposes. 另外,在authClass-> composer.json文件中未指定自动加载以进行测试。

The problem here is that in fact you don't use PSR-4. 这里的问题是,实际上你不使用PSR-4。 In PSR-4 class name should match file name. 在PSR-4中,类名应与文件名匹配。 For db class its fine because db class is located in db.php file, but auth class is located in authClass.php file and that's the problem. 对于db类,它很好,因为db类位于db.php文件中,但auth类位于authClass.php文件中,这就是问题所在。 You should update file name to auth.php 您应该将文件名更新为auth.php

You might need to run: 您可能需要运行:

composer dump-autoload

Also keep in mind in real packages, one vendor package has one namespace, so you don't create multiple namespaces for single package but only single 另外在实际包中请记住,一个供应商包有一个名称空间,因此您不会为单个包创建多个名称空间,而只能创建单个名称空间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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