沃梦达 / 编程问答 / php问题 / 正文

创建包后的 Symfony3 ClassNotFoundException

Symfony3 ClassNotFoundException after bundle creation(创建包后的 Symfony3 ClassNotFoundException)

本文介绍了创建包后的 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.) 创建一个新的 Bundle: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"保罗ArtBundle".您是否忘记了另一个使用"声明命名空间?在 AppKernel.php 中(第 19 行)

Attempted to load class "PaulArtBundle" from namespace "PaulArtBundle". 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 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.

推荐答案

我刚刚安装了 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: PaulArtBundle
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

然后重新启动服务器(也许).那应该解决未找到捆绑类的问题.

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

自从标准将所有内容放在 AppBundle 下以来,我已经有一段时间没有使用 generate:bundle 命令了,所以我不知道这已经破坏"了多长时间.但上周左右至少有三个问题表明这是最近的事情.

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",这让我有点吃惊.原来新的捆绑包覆盖了/路由,这也有点特殊.

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.

如果有人想知道为什么会这样,Symfony 3.2 从

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:创建包后的 Symfony3 ClassNotFoundException

基础教程推荐