我按照 this post 中的所有步骤进行操作,一切似乎都没有问题,因为我可以按照我的预期使用 composer 在供应商文件夹中安装我的新包。问题是当我尝试初始化 AppKernel.php 中的包时,找不到该类。
应用内核.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;
}
这是 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"
}
}
}
和 bundle 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"
}
}
}
也许我错过了什么?
任何帮助将不胜感激。
最佳答案
在 PSR-0 中,你的包结构应该完全反射(reflect)你的命名空间。
因此,为了使其正常工作,请将 bundle 的内容移入/vendor/abdielcs/expanded-collection-bundle/abdielcs/ExpandedCollectionBundle/
代替/vendor/abdielcs/expanded-collection-bundle/
。
或者,使用@OlivierHenry 指出的 target-dir
。
或者,使用 PSR-4 作为几乎所有 3rd 方包,加上 Symfony 推荐的:
来自 Best practices for reusable bundles 。
关于symfony - 未加载自定义供应商包命名空间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36651134/