未加载自定义供应商包命名空间

Custom vendor bundle namespace not loaded

我按照 this post 中的所有步骤进行操作,一切似乎都没有问题,因为我可以像我所期望的那样使用 composer 在 vendor 文件夹中安装我的新包。问题是当我尝试在 AppKernel.php 中初始化包时,找不到 class。

AppKernel.php

public function registerBundles()
    {
        $bundles = array(
            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 Symfony\Bundle\AsseticBundle\AsseticBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new AppBundle\AppBundle(),
            new abdielcs\ExpandedCollectionBundle\ExpandedCollectionBundle(),
        );

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

        return $bundles;
    }

ClassNotFoundException in AppKernel.php line 20: Attempted to load class "ExpandedCollectionBundle" from namespace "abdielcs\ExpandedCollectionBundle". Did you forget a "use" statement for "abdielcs\ExpandedCollectionBundle\ExpandedCollectionBundle"?

这是autoload_namespaces.php

return array(
    'abdielcs\ExpandedCollectionBundle' => array($vendorDir . '/abdielcs/expanded-collection-bundle'),
    'Twig_' => array($vendorDir . '/twig/twig/lib'),
    'Sensio\Bundle\GeneratorBundle' => array($vendorDir . '/sensio/generator-bundle'),
    'Sensio\Bundle\DistributionBundle' => array($vendorDir . '/sensio/distribution-bundle'),
    'SensioLabs\Security' => array($vendorDir . '/sensiolabs/security-checker'),
    'Psr\Log\' => array($vendorDir . '/psr/log'),
    'Doctrine\ORM\' => array($vendorDir . '/doctrine/orm/lib'),
    'Doctrine\DBAL\' => array($vendorDir . '/doctrine/dbal/lib'),
    'Doctrine\Common\Lexer\' => array($vendorDir . '/doctrine/lexer/lib'),
    'Doctrine\Common\Inflector\' => array($vendorDir . '/doctrine/inflector/lib'),
    'Doctrine\Common\Collections\' => array($vendorDir . '/doctrine/collections/lib'),
    'Doctrine\Common\Annotations\' => array($vendorDir . '/doctrine/annotations/lib'),
    'Assetic' => array($vendorDir . '/kriswallsmith/assetic/src'),
);

这是文件夹结构:

这是我的申请composer.json:

{
    "name": "symfony/framework-standard-edition",
    "license": "MIT",
    "type": "project",
    "description": "The \"Symfony Standard Edition\" distribution",
    "autoload": {
        "psr-4": { "": "src/", "SymfonyStandard\": "app/" }
    },
    "require": {
        "php": ">=5.3.3",
        "symfony/symfony": "2.7.*",
        "doctrine/orm": "~2.2,>=2.2.3,<2.5",
        "doctrine/dbal": "<2.5",
        "doctrine/doctrine-bundle": "~1.4",
        "symfony/assetic-bundle": "~2.3",
        "symfony/swiftmailer-bundle": "~2.3",
        "symfony/monolog-bundle": "~2.4",
        "sensio/distribution-bundle": "~3.0,>=3.0.12",
        "sensio/framework-extra-bundle": "~3.0,>=3.0.2",
        "incenteev/composer-parameter-handler": "~2.0",
        "abdielcs/expanded-collection-bundle": "dev-master"
    },
    "require-dev": {
        "sensio/generator-bundle": "~2.3"
    },
    "scripts": {
        "post-root-package-install": [
            "SymfonyStandard\Composer::hookRootPackageInstall"
        ],
        "post-install-cmd": [
            "Incenteev\ParameterHandler\ScriptHandler::buildParameters",
            "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap",
            "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache",
            "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets",
            "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installRequirementsFile",
            "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::removeSymfonyStandardFiles",
            "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::prepareDeploymentTarget"
        ],
        "post-update-cmd": [
            "Incenteev\ParameterHandler\ScriptHandler::buildParameters",
            "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap",
            "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache",
            "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets",
            "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installRequirementsFile",
            "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::removeSymfonyStandardFiles",
            "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::prepareDeploymentTarget"
        ]
    },
    "repositories" : [{
        "type" : "vcs",
        "url" : "https://github.com/abdielcs/ExpandedCollectionBundle.git"
    }],
    "config": {
        "bin-dir": "bin"
    },
    "extra": {
        "symfony-app-dir": "app",
        "symfony-web-dir": "web",
        "symfony-assets-install": "relative",
        "incenteev-parameters": {
            "file": "app/config/parameters.yml"
        },
        "branch-alias": {
            "dev-master": "2.7-dev"
        }
    }
}

和捆绑包 composer.json

{
    "name" : "abdielcs/expanded-collection-bundle",
    "description" : "Work in progress",
    "type" : "symfony-bundle",
    "authors" : [{
        "name" : "Author name",
        "email" : "author email"
    }],
    "keywords" : [
        "collection",
        "expanded"
    ],
    "license" : [
        "MIT"
    ],
    "require" : {
    },
    "autoload" : {
        "psr-0" : {
            "abdielcs\ExpandedCollectionBundle" : ""
        }
    },
    "extra" : {
    "branch-alias" : {
            "dev-master" : "0.1.x-dev"
        }
    }
}

也许我遗漏了什么?。

我们将不胜感激。

我想,target-dir 是必需的。因为你正在使用 psr0

这只是为了支持遗留的 PSR-0 风格的自动加载,所有新代码最好使用不带 target-dir 的 PSR-4,并且鼓励使用带有 PHP 命名空间的 PSR-0 的项目迁移到PSR-4 代替。

在 PSR-0 中,您的包结构应该准确反映您的命名空间。

因此,要使其正常工作,请将您的捆绑包的内容移至
/vendor/abdielcs/expanded-collection-bundle/abdielcs/ExpandedCollectionBundle/

而不是
/vendor/abdielcs/expanded-collection-bundle/.

或者,按照@OlivierHenry 的指示使用target-dir

或者,几乎每个第 3 方捆绑包都使用 PSR-4,而且 Symfony 推荐它:

The PSR-4 autoload standard is recommended for modern bundles

来自 Best practices for reusable bundles.