Symfony3捆绑创建后的ClassNotFoundException [英] Symfony3 ClassNotFoundException after bundle creation

查看:881
本文介绍了Symfony3捆绑创建后的ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Symfony开始一个新的3.3项目,像往常一样开始:

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

1)创建新项目: symfony new ArtProject

2。)创建一个新的软件包: php app / console generate:bundle (Paul / ArtBundle,yml,src /)

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

然后我运行本地服务器,当我打开127.0.0.1:8000我得到这个美丽的消息:

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


(1/1)ClassNotFoundException

尝试加载类PaulArtBundle从命名空间
Paul\ArtBundle。您是否忘记了另一个
命名空间的use语句?在AppKernel.php(第19行)

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

这是奇怪的,我还没有弄清楚为什么这样发生到目前为止。在创建Bundle之前,没有错误;我看到symfony的典型启动。

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.

推荐答案

我刚刚安装了一个新的S3.3.4副本使用以下内容:

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.

当引入新的命名空间时,generate:bundle命令不更新composer.json的autload部分。编辑composer.json和:

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

然后运行

composer dumpautoload

并重新启动服务器(也许)。那应该修复bundle类找不到问题。

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

我没有使用generate:bundle命令是相当一段时间,因为标准是将所有内容都放在AppBundle下,所以我不知道这是多久了破碎。但是上周最少有三个问题表示这是最近的一个。

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.

顺便说一下,当我刷新浏览器时,我得到了Hello World我一点点如果有人想知道为什么会发生这种情况,Symfony 3.2会从

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.

这里有更多详细信息的问题: https://github.com/symfony/symfony-standard/issues/1098

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

看起来维护者喜欢微小的速度改进打破现有的命令。好吧。 Symfony Flex应该使一切都变得很好。

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.

这篇关于Symfony3捆绑创建后的ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆