AppBundle symfony 4

2019-09-21 09:18发布

问题:

I have a problem with Symfony 4, I want generate entities from an existing database with the command :

php bin/console doctrine:mapping:import --force AppBundle xml

But an error appears :

Bundle "AppBundle" does not exist or it is not enabled. Maybe you forgot to add it in the registerBundles() method of your App\Kernel.php file?

I try to import in the file kernel.php in registerBundles() :

new AppBundle/AppBundle();

but undefined class and when I create it in src/AppBundle/AppBundle.php :

<?php namespace AppBundle; use Symfony\Component\HttpKernel\Bundle\Bundle; class AppBundle extends Bundle { }

nothing change and when I retry the command :

PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "AppBundle" from namespace "App". Did you forget a "use" statement for another namespace? in /var/www/html/symfony/Mission3/src/Kernel.php:30

I would like to know if it's possible to create a bundle like that or if it exists an other command to generate entities from an existing database.

回答1:

in SF4, The main application component is named 'App' by default and is not a bundle. You have to manually create a dummy bundle under src\ directory in order to use old doctrine:command related to bundle. see how to manually create a bundle here

After that, you can import/generate mappings/entities using doctrine:commands and use them in the App main module space, or in those bundles.

The SF4 maker tool don't provide bundle creation yet ... But I suppose the SF4 developers consider it's not an urge must have requirement, because they want us to focus on putting most code in the bundle less App main module.



回答2:

In symfony 4 'AppBundle' is doesn't exist anymore. Try use 'App' instead 'AppBundle'