Symfony 2.8:尝试在AppKernel中注册自定义包会引发ClassNotFoundException

时间:2018-11-27 14:17:03

标签: symfony namespaces composer-php symfony-2.8

我开始使用Symfony,并试图添加可重用的包。为了做到这一点,我做了以下工作: 1.did app / console generate:bundle ,这是控制台输出

Bundle namespace: Wyzen/DocumentUploader
 The namespace must end with Bundle. 
Bundle namespace: Wyzen/DocumentUploaderBundle

In your code, a bundle is often referenced by its name. It can be the
concatenation of all namespace parts but it's really up to you to come
up with a unique name (a good practice is to start with the vendor name).
Based on the namespace, we suggest WyzenDocumentUploaderBundle.

Bundle name [WyzenDocumentUploaderBundle]: WyzenDocumentUploaderBundle

Bundles are usually generated into the src/ directory. Unless you're
doing something custom, hit enter to keep this default!

Target Directory [src/]: vendor/

What format do you want to use for your generated configuration?

Configuration format (annotation, yml, xml, php) [xml]: yml


  Bundle generation  


> Generating a sample bundle skeleton into vendor/Wyzen/DocumentUploaderBundle OK!
> Checking that the bundle is autoloaded: FAILED
> Enabling the bundle inside app/AppKernel.php: OK
> Importing the bundle's routes from the app/config/routing.yml file: OK

然后我在bundle根目录下创建了一个composer json,内容如下:

{
    "name": "wyzen/wyzen-document-uploader",
    "description": "A test bundle",
    "type": "library",
    "version":"dev-master",
    "source": {
        "url": "https://github.com/xyz/wyzen-document-uploader",
        "type": "git",
        "reference": "master"
    },
    "autoload": {
        "psr-0": {
            "Wyzen\\DocumentUploaderBundle\\": ""
        }
    },
    "target-dir": "Wyzen/wyzen-document-uploader",
    "authors": [
        {
            "name": "xyz",
            "email": "xyz@xyz.com"
        }
    ],
    "require": {}
}

现在,使用composer在另一个应用程序中成功安装此捆绑包之后。

我添加了以下代码行,以便在AppKernel.php中注册我的捆绑软件

new Wyzen\DocumentUploaderBundle\WyzenDocumentUploaderBundle()

在其上引发此异常

  AppKernel.php第40行中的

ClassNotFoundException:尝试加载   命名空间中的类“ WyzenDocumentUploaderBundle”   “ Wyzen \ DocumentUploaderBundle”。您是否忘记了“使用”声明   另一个名称空间?

2 个答案:

答案 0 :(得分:1)

您描述的所有内容都是正确的。 我安装了您的捆绑包,它可以正常工作。

$ composer require wyzen/wyzen-document-uploader
Using version dev-master for wyzen/wyzen-document-uploader     
...          
Package operations: 1 install, 0 updates, 0 removals
  - Installing wyzen/wyzen-document-uploader (dev-master master): Cloning master from cache

检查

$ composer info -i
...
wyzen/wyzen-document-uploader        dev-master master A test bundle

注册捆绑包

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...
            new Wyzen\DocumentUploaderBundle\WyzenDocumentUploaderBundle(),
        ]

检查

php bin/console debug:config WyzenDocumentUploaderBundle

Current configuration for "WyzenDocumentUploaderBundle"
=======================================================

wyzen_document_uploader: {  }

要解决您的问题,请尝试重建自动装带器:

composer run-script post-update-cmd

或重试全新安装

答案 1 :(得分:0)

最后通过在自动加载树下将 classmap 添加到composer json并将路径传递到主项目中的class目录来解决了该问题。

 "classmap": [
        "vendor/wyzen/wyzen-document-uploader"
 ]