简体   繁体   English

Composer Autoload 仅在根级目录中查找类

[英]Composer Autoload only finding classes in root-level directories

I am currently working on building an API in the Slim framework, and having a bit of trouble with it not finding classes that should be getting autoloaded.我目前正在 Slim 框架中构建一个 API,并且在找不到应该自动加载的类时遇到了一些麻烦。

Folder structure文件夹结构

database
- db_connection.php

<?php
namespace Database;

class DBConnection {}

...

public
- index.php
src
- middlewares
-- authentication_middleware.php

<?php
namespace Middleware;

class AuthenticationMiddleware {}

...

- routes
-- different routes for tickets, projects, notifications etc.
- utils
-- data_convertor.php

<?php
namespace Utils;

class DataConvertor {}

...

And here is my composer.json :这是我的composer.json

{
  "autoload": {
    "psr-4": {
      "Database\\": "./database",
      "Middleware\\": "./src/middlewares",
      "Utils\\": "./src/utils"
    }
  },
  "require": {
    // Dependencies
  }
}

Now if I go into my projects.php file in src/routes and use Database\DBConnection , then do $db_connection = new DBConnection();现在,如果我src/routes进入我$db_connection = new DBConnection(); projects.php use Database\DBConnection further down, all is well, my database connects and the app returns a response.再往下,一切都很好,我的数据库连接并且应用程序返回响应。 However, now that I have attempted to create these other namespaces, Middleware and Utils, I am suddenly getting "Uncaught Error: Class "Middleware\AuthenticationMiddleware" not found".但是,既然我已经尝试创建这些其他命名空间、中间件和实用程序,我突然收到“未捕获的错误:Class“Middleware\AuthenticationMiddleware”未找到”。 It is in Slim 4 so here is what it ultimately looks like:它在 Slim 4 中,所以它最终的样子如下:

<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Database\DBConnection;
use Middleware\AuthenticationMiddleware;


$app->group('/projects', function($app) {
  $app->get('', function(Request $request, Response $response, Array $args) {
    try {
      $db_connection = new DBConnection();
      // The rest of the code
    return $response->withStatus($status);
  });
})->add(new AuthenticationMiddleware()); // Here is where the "error" is generated.

What on Earth could I be doing wrong?我到底做错了什么? I have gone with the theory that Middleware might be a protected word so changed it to something else, to no avail, and even ended up renaming Middleware to MW and AuthenticationMiddleware to AM to eliminate any chance of mispelling, again to no avail.我已经接受了中间件可能是一个受保护的单词的理论,因此将其更改为其他内容,但无济于事,甚至最终将中间件重命名为 MW,将 AuthenticationMiddleware 重命名为 AM,以消除任何拼写错误的机会,但同样无济于事。 Between every step I have also been running composer update and even composer dumpautoload && composer update to try and get it working.在每一步之间,我也一直在运行composer update甚至composer dumpautoload && composer update来尝试让它工作。

As the title may suggest, the one thing the non-loading ones have in common, and separate from the loading one, is that database is a top-level directory and the others aren't.正如标题所暗示的那样,非加载目录的共同点是与加载目录不同的是, database是顶级目录,而其他目录则不是。 But surely as long as the path is correct in composer.json that shouldn't matter?但是,只要composer.json中的路径是正确的,那肯定没关系?

Further to this, a bit more Googling before posting has led me to look at vendor/composer/autoload_psr4.php and sure enough, it returns the below:除此之外,在发布之前进行更多谷歌搜索让我查看了vendor/composer/autoload_psr4.php果然,它返回以下内容:

return array(
    'Utils\\' => array($baseDir . '/src/utils'),
    'Middleware\\' => array($baseDir . '/src/middlewares'),
    'Database\\' => array($baseDir . '/database'),
    // Plenty of auto-generated ones with plenty of slashes in the directory paths
)

Thanks to the comments on my question I now know that the issue was that the class name eg AuthenticationMiddleware didn't match the filename, in this case authentication_middleware.php , changing that by renaming the files has fixed the issue.感谢对我的问题的评论,我现在知道问题是 class 名称,例如AuthenticationMiddleware与文件名不匹配,在这种情况下authentication_middleware.php ,通过重命名文件来改变它已经解决了问题。 Weird that the database connection was still working despite that - but hey, it now all works so I'm good!奇怪的是,尽管如此,数据库连接仍在工作-但是,嘿,现在一切正常,所以我很好!

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

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