Symfony3 ClassNotFoundException after bundle creation Symfony3 ClassNotFoundException after bundle creation symfony symfony

Symfony3 ClassNotFoundException after bundle creation


I just installed a fresh copy of S3.3.4 (latest version as of this writing) using:

composer create-project symfony/framework-standard-edition s334 "3.3.4"bin/console generate:bundleShare across multiple apps: yesnamespace: Paul\ArtBundlebundle name: PaulArtBundleTarget Directory: src/

Refreshed the browser and sure enough I got the class not found message.

The generate:bundle command is not updating the autload section of composer.json when a new namespace is introduced. Edit composer.json and:

# composer.json"autoload": {    "psr-4": {        "AppBundle\\": "src/AppBundle",        "Paul\\": "src/Paul"    },    "classmap": [ "app/AppKernel.php", "app/AppCache.php" ]},

Then run

composer dumpautoload

And restart the server(maybe). That should fix the bundle class not found problem.

I have not used the generate:bundle command is quite some time ever since the standard was to put everything under AppBundle so I don't know how long this has been "broken". But at least three questions in the last week or so indicates it was something recent.

And by the way, when I refreshed the browser I got "Hello World" which threw me for a bit. Turns out the new bundle overrides the / route which is also sort of special.

And in case anybody is wondering why this started happening, Symfony 3.2 changed from

#composer.json"psr-4": { "": "src/" },To"psr-4": { "AppBundle\\": "src/AppBundle" },

You could always just change it back though I think spelling out individual namespaces might be "better". Not sure.

And here is an issue with more details: https://github.com/symfony/symfony-standard/issues/1098

Looks like the maintainer favored a tiny speed improvement over breaking an existing command. Oh well. Symfony Flex is supposed to make everything great again.


If you generate a bundle for usage in multiple projects (with own namespace) you need to add it in the composer.json as follwed:

Lets assume your bundle name is CompanyFooBundle with namespace Company\Bundle\FooBundle then the composer autoload section should look like:

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


This works for me in:

Generate your Bundle with

./console generate:bundle

And follow the steps as always, now, do what you want in your composer.json file with the line

"AppBundle\\": "src/AppBundle"

Replace with "": "src/" or add your bundle, for example: "BackendBundle\\": "src/BackendBundle"

Here's the new part:

  1. Install composer in your bin directory, copy and paste the steps from https://getcomposer.org/download/

  2. Up a level in your project directory, and in your root folder (of your project) run the next command

php ./bin/composer.phar update