提问者:小点点

如何使用Composer自动加载自制的Symfony 4捆绑包


在一个使用symfony4的项目中,我使用composer导入了一个自制的包

"require-dev": {
    "symfony/dotenv": "^4.0",
    "symfony/profiler-pack": "^1.0",
    "myusername/my-bundle": "dev-master"
},
"repositories" : [{
    "type" : "vcs",
    "url" : "git@github.com:myusername/my-bundle.git"
}],

捆绑包已正确安装,但出现以下错误:

PHP致命错误:未捕获的Symfony\组件\调试\异常\ClassNotFoundExcture:试图从命名空间"My\Bundle\MyBundle"加载类"MyBundle"。

捆绑包从捆绑包加载到内核中。php文件

My\Bundle\MyBundle\MyBundle::class => ['dev' => true, 'test' => true]

给你作曲家。我的捆绑包的json:

{
  "name" : "myusername/my-bundle",
  "type" : "symfony-bundle",
  "autoload" : {
    "psr-4" : {
      "myusername\\MyBundle\\" : ""
    }
  }
}

最后这个包的主文件是MyBundle.php

<?php
namespace My\Bundle\MyBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use My\Bundle\MyBundle\DependencyInjection\MyExtension;

class MyBundle extends Bundle
{
    public function getContainerExtension()
    {
        return new MyExtension();
    }
}

我知道问题是由自动装弹机引起的,但我不知道如何解决它。我尝试了composer dump autoload命令,并参考了以下类似问题,但运气不佳:

  • Symfony自动加载不适用于自定义捆绑包
  • 编写器未为库生成自动加载
  • 找不到PHP编写器自动加载PSR-4类

非常感谢。


共1个答案

匿名用户

只是composer。json

"autoload": {
        "psr-4": {
            "": "src/"
        }
    },