2016-04-15 72 views
4

我按照this post所有步骤和所有似乎是好的,因为我可以在供应商的文件夹作曲家安装我的新包,因为我espected。问题是当我尝试初始化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; 
    } 

的ClassNotFoundException在AppKernel.php线20:试图加载 类 “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'), 
); 

这是文件夹结构:

enter image description here

这是我的应用程序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" 
     } 
    } 
} 

而且bu ndle 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" 
     } 
    } 
} 

也许我错过了什么?

任何帮助将不胜感激。

+0

显示你捆绑composer.json –

+0

做,问题更新。 – abdiel

+0

不是应用程序composer.json,供应商包的composer.json –

回答

3

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

因此,为了它的工作原理移动你的包的内容
/vendor/abdielcs/expanded-collection-bundle/abdielcs/ExpandedCollectionBundle/

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

或者,使用target-dir通过@OlivierHenry如指出。

或者使用PSR-4因为几乎每家第三方软件包,以及它的建议通过的Symfony:

的PSR-4自动加载标准被推荐用于现代束

Best practices for reusable bundles

+0

谢谢,使用psr-4为我工作。最后还需要一些\\。 “abdielcs \\ ExpandedCollectionBundle \\”:“” – abdiel

+0

不客气。而且,是的,'\\\'在psr-4中是强制性的,我只是看到它在你的问题中缺失。 – chalasr

3

我认为,需要目标目录。因为您正在使用psr0

这仅用于支持传统的PSR-0样式自动加载,并且所有新代码应该最好使用PSR-4而不使用目标代码,并且使用PSR-0的项目鼓励PHP命名空间迁移到PSR-4代替。

+0

感谢您的回答,我只会选择@chalasr答案,因为它似乎有更多解释。 – abdiel