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:bundle
Share across multiple apps: yes
namespace: Paul\ArtBundle
bundle name: PaulArtBundle
Target 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.
ClassNotFoundException when loading local Symfony bundle
After every manual update of autoload
section in your composer.json
:
{
"autoload": {
"psr-4": {
"Namespace\\CoreBundle\\": "src/Namespace/CoreBundle"
}
}
}
don't forget to refresh your generated /vendor/autoload.php
via CLI:
composer dump
# shortcut for `composer dump-autoload`
Symfony 3.4 : generate bundle : ClassNotFoundException: Attempted to load class MyVendorFOSUserBundle from namespace MyVendorFOSUserBundle
Finally, I found the solution:
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle",
"MyVendorFOSUserBundle\\": "src/MyVendor/MyVendorFOSUserBundle" //Line added
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
Don't forget to run:
composer dumpautoload
Symfony3 error adding new bundle
My solution was generating aoutoloaders again like
php composer.phar dump-autoload
Hope it helps someone
How to auto generate a bundle in Symfony3?
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
Symfony switch from 2.8-3.3 ClassNotFoundException use statement missing namespace?
All Credit goes to Cerad the answer is sadly duplicated however I did not notice something that was needed. the autoloader needs to know where it is pulling classes from and has to be setup with the new way the symfony does it now. Here is my example with my additional code to make it work.
"autoload" : {
"psr-4": {
"AppBundle\\": "src/AppBundle",
"BundleName\\": "src/BundleName"
},
"classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
},
Related Topics
Simultaneous Requests to PHP Script
PHP Reading Shell_Exec Live Output
How to Properly Url Encode a String in PHP
A Non Well Formed Numeric Value Encountered
Storing Files in Database VS File System
PHP Associative Array Duplicate Keys
MySQLi Equivalent of MySQL_Result()
How to Create a Simple 'Hello World' Module in Magento
Create Word Document Using PHP in Linux
Laravel: Syntax Error or Access Violation: 1055 Error
How to Increase Maximum Execution Time in PHP
Show Ajax Upload Status on Progress Element