首页 > 解决方案 > 未找到自己的包/类中的 Composer 自动加载

问题描述

我现在尝试了一些不同的方法并在这里和其他地方阅读文章,但我无法弄清楚我做错了什么。我正在开发一个包,我希望它通过符号链接安装在不同的项目中,以同时开发这两个部分,而无需一遍又一遍地更新依赖项。我无法工作的是自动加载。

这是我的设置:

项目作曲家.json

{
  "repositories": [
    {
        "type": "path",
        "url": "../../FormTableComponent",
        "options": {
            "symlink": true
        }
    }
  ],
  "require": {
    [...]
    "bluechord/formtablecomponent": "@dev"
  }
}

FormtableComponent 文件夹结构

FormTableComponent/
  src/
  | Container.php
  composer.json

FormtableComponent composer.json

{
  "name": "bluechord/formtablecomponent",
  "description": "...",
  "autoload": {
    "psr-4": {
      "BlueChord\\FormTableComponent\\": "src/"
    }
  },
  "require" : {
    [...]
  }
}

FormtableComponent Container.php

<?php

/**
 * FormTable Container. The Container Class that registers all Parts of the 
 * Component.
 */

 namespace BlueChord\FormTableComponent;

 class Container {

    function __construct() {

    }
 }

当我尝试使用该类并实例化它时,我得到

Uncaught Error: Class 'BlueChord\FormTableComponent\Container' not found

谢谢你的帮助!

附加信息:

一个简单的 Test.php 重现项目内部的错误

<?php

require_once 'bin/vendor/autoload.php';
echo "BCOSP Test";

use DebugBar\StandardDebugBar;
use BlueChord\FormTableComponent\Container;

$debugbar = new StandardDebugBar(); --> WORKS
echo StandardDebugBar::class ."\n";

$container = new Container();  --> ERROR
echo Container::class . "\n";

作曲家

vendor/composer/autoload_psr4.php 不包含我的包的任何数组键。

如果我composer dump-autoload在我的主项目中运行,则没有任何变化。如果我composer dump-autoload在我的包中运行它会创建正确的 autoload_psr4.php

标签: composer-phpclassnotfoundexceptionautoload

解决方案


推荐阅读