Silex 命令失败

Silex command fail

当我尝试使用 Silex 运行 控制台命令时出现此错误。

PHP Error:  Class 'Testing\Command\TestingCommand' not found in /var/www/testCmd/app/console on line 9
PHP Stack trace:
PHP   1. {main}() /var/www/testCmd/app/console:0
PHP Fatal error:  Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "TestingCommand" from namespace "Testing\Command".
Did you forget a "use" statement for another namespace? in /var/www/testCmd/app/console:9
Stack trace:
#0 {main}
  thrown in /var/www/testCmd/app/console on line 9

我有 app/console.php 和 app/bootstrap.php 文件。控制台正在加载 bootstrap 并且在控制台文件中我有一些类似的东西:

#!/usr/bin/env php
<?php

set_time_limit(0);

$app = require_once __DIR__ . '/bootstrap.php';

$application = $app['console'];
$app['console']->add(new \Testing\Command\TestingCommand());
$application->run();

作曲家文件

{
    "name": "testing/Command",
    "require": {
        "knplabs/console-service-provider": "^2.0",
        "silex/silex": "^2.0",
        "symfony/monolog-bridge": "^3.1",
        "doctrine/common": "^2.6",
        "doctrine/dbal": "^2.5"
    },
    "autoload": {
        "psr-4": {
            "\": "src/"
        }
    }
}

命令位于src/Command/TestingCommand.php

我是 Silex 的新手,我不知道是什么导致了这个问题。谢谢

自动加载器无法加载命令 class。根据 composer.json 和 class 的自动加载部分,具有此 class 的名称文件应位于 src/Testing/Command/TestingCommand.php 中。所以你可以把这个文件移动到这个位置或者在 composer.json:

中设置另一个搜索目录
"autoload": {
    "psr-4": {
        "Testing\Command\": "src/Command/"
        "\": "src/"
    }
}

改变后composer.json运行composer dump-autoload

https://getcomposer.org/doc/01-basic-usage.md#autoloading