34

I wanted to start a new 3.3 project in Symfony and started as usual:

1.) Creating the new project: symfony new ArtProject

2.) Creating a new Bundle: php app/console generate:bundle (Paul/ArtBundle, yml, src/)

Then I run the local server and when I open 127.0.0.1:8000 I get this beautiful message:

(1/1) ClassNotFoundException

Attempted to load class "PaulArtBundle" from namespace "Paul\ArtBundle". Did you forget a "use" statement for another namespace? in AppKernel.php (line 19)

Which is strange and I haven't figured out why this happen so far. Before creating the Bundle, there was no error; I saw the typical startpage of symfony.

public function registerBundles()
{
    $bundles = [
        new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
        ......
        new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
        new AppBundle\AppBundle(),
        new Paul\ArtBundle\PaulArtBundle(),
    ];
}

<?php

namespace Paul\ArtBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class PaulArtBundle extends Bundle
{
}

Any idea whats going on there? I did not change a thing, I only ran these commands.

12
  • Check AppKernel.php , is it correct ? Commented Jul 6, 2017 at 10:53
  • @SamJanssens yes, I also can jump to the php file in my IDE fromt he kernel Commented Jul 6, 2017 at 10:54
  • Did you well registred the new Bundle @ Appkernel.php file ? Commented Jul 6, 2017 at 10:55
  • well there is clearly a namespace issue going on somewhere, but where ... Commented Jul 6, 2017 at 10:56
  • 1
    Seem to be getting a rash of these questions. Check the autoload section of your composer.json file: stackoverflow.com/questions/44928982/… Might be something screwy with the latest release. Commented Jul 6, 2017 at 11:53

4 Answers 4

72

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Yup, worked, after is restarted the server. Tahnks :)
awesome work there. Thanks for the answer. Spent days literally fighting this.
I just posted this question stackoverflow.com/questions/45151067/… which you marked as duplicate and yes, this one is quite similar only I was going from 2.8.9 to 2.8.24 (in which the psr-4 entry is already different) and did what was instructed here except the composer dumpautoload line which is off the table for me as I mentioned in my question, can you indicate a workaround for this, thanx
Is this really changed in Symfony3 that a new Bundle must have to be in the composer.json added?
@MiracleJohnson - You can just change the psr4 line back to { "": "src/" }, if you want. Slight performance hit but not enough to notice.
3

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"
    ]
},
...

1 Comment

Thanks for the "Company\\Bundle\\FooBundle\\", my mistake was : "CompanyBundleFooBundle\\"
0

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

Comments

0

Removing vendor dir and again running composer install helped me with same problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.