Search code examples
symfonygithubbundle

Custom vendor bundle namespace not loaded


I follow all the steps in this post and all seem to be ok, since I could install my new bundle in vendor folder with composer as I espected. The problem is when I try to intialize the bundle in AppKernel.php the class was not found.

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"?

this is the 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'),
);

This is the folders structure:

enter image description here

This is my application 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"
        }
    }
}

And the 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"
        }
    }
}

Maybe I'm missing something?.

Any help will be appreciated.


Solution

  • In PSR-0, your bundle structure should exactly reflect your namespace.

    So, to make it works move the content of your bundle in
    /vendor/abdielcs/expanded-collection-bundle/abdielcs/ExpandedCollectionBundle/

    Instead of
    /vendor/abdielcs/expanded-collection-bundle/.

    Or, use target-dir as pointed by @OlivierHenry.

    Or, use PSR-4 as almost every 3rd party bundles, plus it's recommended by Symfony:

    The PSR-4 autoload standard is recommended for modern bundles

    From Best practices for reusable bundles.