简体   繁体   English

PSR-4 自动加载器问题

[英]PSR-4 autoloader issue

I have trouble seeing what am I doing wrong with autoloader.我很难看出我在使用自动加载器时做错了什么。 My folder structure is as follows:我的文件夹结构如下:

| - src/
|    - Files/
|        - Bla.php
|    - Models/
|        - ...
| - vendor/
|     - ...
| composer.json

And composer.json autoload part looks like this: composer.json自动加载部分如下所示:

"autoload": {
    "psr-4": {
        "Migrations\\" : "src/"
    }
}

Now the Bla.php looks like this:现在Bla.php看起来像这样:

<?php

namespace Migrations\Files;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Bla extends AbstractMigration
{
    ...
}

And I am getting the error:我收到错误:

Fatal error: Class 'Doctrine\Migrations\AbstractMigration' not found in /var/www/html/migrations/src/Files/Bla.php

When I look at the vendor folder, package is there.当我查看vendor文件夹时,包就在那里。 From my IDE also indexing works fine so that I can CMD+click to the AbstractMigration file without problem.从我的 IDE 中,索引也可以正常工作,因此我可以通过 CMD+单击到AbstractMigration文件而不会出现问题。

I have tried deleting the vendor folder, clearing composer cache, doing a dump-autoload and reinstalling all packages, but nothing seems to work.我尝试删除vendor文件夹,清除作曲家缓存,执行转储自动加载并重新安装所有软件包,但似乎没有任何效果。

Where am I making the mistake?我在哪里犯了错误?

You need to include the Composer autoload file otherwise your application doesn't know what classes exist.您需要包含 Composer 自动加载文件,否则您的应用程序不知道存在哪些类。 It is a file created by composer when you install the dependencies, a lot of frameworks that use Composer will include this file for you automatically, but if you are not using a framework you will need to include the file yourself.它是 composer 在安装依赖时创建的文件,很多使用 Composer 的框架会自动为你包含这个文件,但如果你不使用框架,则需要自己包含该文件。

require __DIR__ . '/vendor/autoload.php';

Where you need to put it will depend on your application but its best to load as early as possible, if you have a bootstrap file then that would be the place to put it.你需要把它放在哪里取决于你的应用程序,但最好尽早加载,如果你有一个引导文件,那么这就是放置它的地方。

You can read about it here你可以在这里阅读

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

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