Symfony: Fatal Error: Class not found in app/console

Symfony: Fatal Error: Class not found in app/console

我正在尝试从命令行获取命令以使用 symfony 工作。我有以下文件夹结构:

现在在我的控制台文件中有以下代码:

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

require_once '/../vendor/autoload.php';

use Source\Commands\TwitterCommand;
use Symfony\Component\Console\Application;
use Endroid\Twitter\Twitter;

//API Keys
...

$client = new Twitter($consumerKey, $consumerSecret, $accessToken,   $accessTokenSecret);

$application = new Application();
$application->add(new TwitterCommand($client));
$application->run();

然后在我的 TwitterCommand.php 我有以下内容:

<?php 

namespace Source\Commands;

use Twitter;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;

class TwitterCommand extends Command
{
private $client;

public function __construct(Twitter $client)
{
    parent::__construct();
    $this->client = $client;
}

protected function configure()
{
    $this
        ->setName('freq:tweet')
        ->setDescription('Show the tweets')
}

protected function execute(InputInterface $input, OutputInterface $output)
{

    // Retrieve the user's timeline
    $tweets = $twitter->getTimeline(array(
        'count' => 5
    ));

    // Or retrieve the timeline using the generic query method
    $response = $twitter->query('statuses/user_timeline', 'GET', 'json', $parameters);
    $tweets = json_decode($response->getContent());

    $output->writeln($tweets);
}
}

问题是我遇到了“致命错误:Class 'Source\Commands\TwitterCommand' not found in C:...”,错误是在这一行中发送的:

$application->add(new TwitterCommand($client));

知道我做错了什么吗?

如果您希望 class 自动加载,则必须休假 PSR-0 标准。在您的项目的 composer.json 中,您可能已经定义了类似的内容:

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

您的 class 文件路径应该是 src/Source/Commands/TwitterCommand.php