【问题标题】:Custom vendor bundle namespace not loaded未加载自定义供应商捆绑名称空间
【发布时间】:2016-08-07 15:53:11
【问题描述】:

我按照this post 中的所有步骤进行操作,似乎一切正常,因为我可以按照我的预期将我的新包安装在带有 composer 的 vendor 文件夹中。问题是当我尝试初始化 AppKernel.php 中的包时,找不到该类。

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;
    }

AppKernel.php 第 20 行中的 ClassNotFoundException:尝试加载 命名空间中的类“ExpandedCollectionBundle” “abdielcs\ExpandedCollectionBundle”。您是否忘记了“使用”声明 对于“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"
        }
    }
}

也许我错过了什么?

任何帮助将不胜感激。

【问题讨论】:

    标签: symfony github bundle


    【解决方案1】:

    我认为,target-dir 是必需的。因为你用的是psr0

    这只是为了支持旧的 PSR-0 样式自动加载,所有新代码最好使用不带 target-dir 的 PSR-4,鼓励使用 PSR-0 和 PHP 命名空间的项目迁移到 PSR-4。

    【讨论】:

    • 感谢您的回答,我将选择@chalasr 回答,只是因为它似乎解释得更多。
    【解决方案2】:

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

    因此,为了使其正常工作,请将捆绑包的内容移动到
    /vendor/abdielcs/expanded-collection-bundle/abdielcs/ExpandedCollectionBundle/

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

    或者,使用@OlivierHenry 指出的target-dir

    或者,几乎所有 3rd 方捆绑包都使用 PSR-4,而且 Symfony 推荐它:

    现代捆绑包推荐使用 PSR-4 自动加载标准

    来自Best practices for reusable bundles

    【讨论】:

    • 谢谢,使用 psr-4 为我工作。最后也需要一些 \\ 。 “abdielcs\\ExpandedCollectionBundle\\”:“”
    • 不客气。而且,是的 `\\` 在 psr-4 中是强制性的,我刚刚看到您的问题中缺少它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-10
    • 1970-01-01
    相关资源
    最近更新 更多