Search code examples
phpsymfonybundleauto-generate

How to auto generate a bundle in Symfony3?


I've tried to automatically generate a bundle in Symfony3, using instructions on the official site: https://symfony.com/doc/current/bundles.html, but failed.

I created a directory \src\TestBundle in my project with a file TestBundle.php in it. I extended my class TestBundle from Bundle and added the use Symfony\Component\HttpKernel\Bundle\Bundle; line before the class declaration.

I added my new bundle name to the bundles list in AppKernel.php.

Finally, when I execute $ php bin/console generate:bundle --namespace=TestBundle, I get this error:

PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "TestBundle" from namespace "TestBundle".

Any ideas why that happens?


Solution

  • If you launch this command:

    php bin/console generate:bundle --namespace=TestBundle
    

    Symfony create the Bundle for you, you don't need to create file and add It into the AppKernel file.

    Try to remove all your files and reference about TestBundle and after launch only the command, Symfony will create and register the Bundle for you.

    Instead i you want to create manually thos file you need to create the directory \src\TestBundle and inside It file TestBundle.php. After you need to register It into your AppKernel. After you need to tell to composer to load that bundle, if is not already done use this configuration for autoload inside composer.json

    "autoload": {
        "psr-4": {
            "": "src/"
        },
        "classmap": [
            "app/AppKernel.php",
            "app/AppCache.php"
        ]
    },
    

    And finally launch the command inside your console:

    composer dump-autoload