Symfony 3.3.5 - 无法生成新捆绑包

时间:2017-07-22 08:51:37

标签: symfony

我刚刚安装了新的Symfony 3.3.5项目。 现在我正在尝试生成新的包

php bin/console generate:bundle

但得到这个:

> Generating a sample bundle skeleton into app/../src/Web/BaseBundle
  created ./app/../src/Web/BaseBundle/
  created ./app/../src/Web/BaseBundle/WebBaseBundle.php
  created ./app/../src/Web/BaseBundle/Controller/
  created ./app/../src/Web/BaseBundle/Controller/DefaultController.php
  updated ./app/../tests/WebBaseBundle/Controller/DefaultControllerTest.php
  created ./app/../src/Web/BaseBundle/Resources/views/Default/
  created ./app/../src/Web/BaseBundle/Resources/views/Default/index.html.twig
  created ./app/../src/Web/BaseBundle/Resources/config/
  created ./app/../src/Web/BaseBundle/Resources/config/services.yml
  created ./app/../src/Web/BaseBundle/Resources/config/routing.yml
> Checking that the bundle is autoloaded
FAILED
> Enabling the bundle inside app/AppKernel.php
  updated ./app/AppKernel.php
OK

所以在composer.json中修复它是这样的:

"autoload": {
    "psr-4": { "": "src/" },
    "classmap": [
        "app/AppKernel.php",
        "app/AppCache.php"
    ]
},

也尝试过这样:

"autoload": {
    "psr-4": { "WebBaseBundle": "src/Web/BaseBundle" },
    "classmap": [
        "app/AppKernel.php",
        "app/AppCache.php"
    ]
},

但如果我尝试启动我的项目,我就会收到此错误:

  

Symfony \ Component \ Debug \ Exception \ ClassNotFoundException:试图   从名称空间“Web \ BaseBundle”加载类“WebBaseBundle”。你是否   忘记另一个命名空间的“use”语句?在   第18行的/Volumes/U/Projects/e-shop/app/AppKernel.php

这是我的AppKernel

public function registerBundles()
{
    $bundles = [
        new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
        new Symfony\Bundle\SecurityBundle\SecurityBundle(),
        new Symfony\Bundle\TwigBundle\TwigBundle(),
        new Symfony\Bundle\MonologBundle\MonologBundle(),
        new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
        new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
        new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
        new Web\BaseBundle\WebBaseBundle(),
    ];

    if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
        $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
        $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
        $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();

        if ('dev' === $this->getEnvironment()) {
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
            $bundles[] = new Symfony\Bundle\WebServerBundle\WebServerBundle();
        }
    }

    return $bundles;
}

全新项目可能出现什么问题? 谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

要修复此错误,您必须编辑composer.json:

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

并在命令运行后:

composer install (if composer is installed)

or 

php composer.phar (if you work with composer.phar)