在 AppKernel.php 中找不到类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17584041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:08:18  来源:igfitidea点击:

Class Not found in AppKernel.php

phpsymfonydeploymentnamespaces

提问by Jason Lin

I'm trying to deploy my Symfony2 project. When I run the command

我正在尝试部署我的 Symfony2 项目。当我运行命令时

php app/console cache:clear --env=prod --no-debug

I get the following error:

我收到以下错误:

PHP Fatal error: Class 'Acme\MainBundle\AcmeMainBundle' not found in /var/www/html/app/AppKernal.php on line 24

This is the in AppKernal.php

这是在 AppKernal.php

public function registerBundles()
{
    $bundles = array(
        ...
        new Acme\MainBundle\AcmeMainBundle(),
    );
    ...
}

It seems like there's a problem with the namespace?

好像命名空间有问题?

回答by Praveesh

If you are getting a bundle not found error in Symfony, in composer.json, modify the psr-4section under autoloadsection like this.

如果您在 Symfony 中遇到 bundle not found 错误,请在 composer.json 中像这样修改psr-4部分下的autoload部分。

"autoload": {
    "psr-4": {
        "": "src/"
    },
},

By doing so, you don't have to explicitly add the new bundle namespaces when you create a new bundle.

通过这样做,您不必在创建新包时显式添加新包命名空间。

回答by chamal101

If this happened in Symfony 3 (I haven't tested this in symfony 2),

如果这发生在 Symfony 3(我没有在 symfony 2 中测试过),

  1. Make sure your bundle is registered in AppKernal.php as:

    public function registerBundles()
    {
        $bundles = array(
            ...
            new YourBundle\YourBundle(),
        );
        ...
    }
    
  2. Check if you've updated composer.json

    "autoload": {
        "psr-4": {
            "AppBundle\": "src/AppBundle",
            "YourBundle\": "src/YourBundle"
        },
        "classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
    },
    
  3. Run composer updatefrom your console

  1. 确保您的包在 AppKernal.php 中注册为:

    public function registerBundles()
    {
        $bundles = array(
            ...
            new YourBundle\YourBundle(),
        );
        ...
    }
    
  2. 检查您是否已更新 composer.json

    "autoload": {
        "psr-4": {
            "AppBundle\": "src/AppBundle",
            "YourBundle\": "src/YourBundle"
        },
        "classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
    },
    
  3. composer update从你的控制台运行

回答by arnaudbey

Got the same problem. I've just deleted my vendor folder

遇到了同样的问题。我刚刚删除了我的供应商文件夹

rm -rf vendor

and relaunch a composer update.. then everything was fine

并重新启动作曲家更新..然后一切都很好

composer update

回答by Moonchild

Had the same problem after adding a bundle with the code generator with Symfony 3.2. I had to add this new bundle in the autoload section of composer.json after the AppBundle :

在使用 Symfony 3.2 添加带有代码生成器的包后遇到了同样的问题。我必须在 AppBundle 之后的 composer.json 的自动加载部分中添加这个新包:

(...)
"autoload": {
    "psr-4": {
        "AppBundle\": "src/AppBundle",
        "CoreBundle\": "src/CoreBundle"
    },
(...)

回答by habibun

Have the same problem?

有同样的问题吗?

sudo rm -rf vendor/

须藤 rm -rf 供应商/

composer install

or

或者

composer update

回答by Jason Lin

Turns out i needed to add this to the autoloader. Thanks @DevZer0's comment.

结果我需要将它添加到自动加载器中。感谢@DevZer0 的评论。

$loader->add('Acme', __DIR__ . '/../src');

$loader->add('Acme', __DIR__ . '/../src');

回答by gabi493

Removing the whole folder wasn't very convincing for me, so I tried doing just the following and it worked:

删除整个文件夹对我来说不是很有说服力,所以我尝试只执行以下操作并且它有效:

$ composer update

Then I checked it with:

然后我检查了它:

$ php bin/console assets:install web --symlink
$ php bin/console cache:clear

回答by edhgoose

I've experience all the above errors before, but I found a similar issue when running php app/console doctrine:migrations:status.

我以前遇到过上述所有错误,但在运行php app/console doctrine:migrations:status.

The issue was that I had not imported a constant which was being used as part of an annotation. For example:

问题是我没有导入用作注释一部分的常量。例如:

/**
 * Either "OUTBOUND" (we send out) or "INBOUND" (we receive).
 *
 * @var string
 *
 * @ORM\Column(name="Direction", type="string", length=20, nullable=true)
 *
 * @Assert\Choice(
 *     choices = {BatchDirections::OUTBOUND,BatchDirections::INBOUND},
 *     message = "Choose a valid direction."
 * )
 */
private $Direction = BatchDirections::INBOUND;

I hadn't imported the BatchDirections file.

我没有导入 BatchDirections 文件。

Resolving the import with use Nora\BatchBundle\Constants\BatchDirections;fixed the issue.

通过use Nora\BatchBundle\Constants\BatchDirections;修复问题解决导入。