douqiao7958 2015-03-28 00:10
浏览 222
已采纳

Symfony:致命错误:在app / console中找不到类

I'm trying to get commands from command line to work using symfony. I have the following folder structure:

  • app console
  • src
    • Source
      • Commands twitterCommand.php

Now in my console file I have the following code:

#!/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();

And then in my TwitterCommand.php i have the following:

<?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);
}
}

The problem is I'm getting a 'Fatal error: Class 'Source\Commands\TwitterCommand' not found in C:...' and the error is sent in this line:

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

Any idea on what am I doing wrong?

  • 写回答

1条回答 默认 最新

  • doue1925 2015-03-28 09:52
    关注

    If you want your class to be autoloaded you have to fallow PSR-0 standard. In the composer.json of your project you have probably defined something like that:

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

    Your class file path should be src/Source/Commands/TwitterCommand.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系