Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

php - Symfony3 ClassNotFoundException after bundle creation

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 "PaulArtBundle". 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 SymfonyBundleFrameworkBundleFrameworkBundle(),
        ......
        new SensioBundleFrameworkExtraBundleSensioFrameworkExtraBundle(),
        new AppBundleAppBundle(),
        new PaulArtBundlePaulArtBundle(),
    ];
}

<?php

namespace PaulArtBundle;

use SymfonyComponentHttpKernelBundleBundle;

class PaulArtBundle extends Bundle
{
}

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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: PaulArtBundle
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...