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

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?

图片转代码服务由CSDN问答提供 功能建议

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

  • app console
  • src
    • 来源 < UL>
    • 命令 twitterCommand.php

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

       #!/ usr / bin / env php 
      &lt;?php 
       
      require_once'/../vendor/autoload  .php'; 
       
      use Source \ Commands \ TwitterCommand; 
      use Symfony \ Component \ Console \ Application; 
      use Endroid \ Twitter \ Twitter; 
       
       // API密钥
       ... 
       
       $  client = new Twitter($ consumerKey,$ consumerSecret,$ accessToken,$ accessTokenSecret); 
       
       $ application = new Application(); 
       $ application-&gt; add(new TwitterCommand($ client)); 
       $  application-&gt; run(); 
         
       
       

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

       &lt;?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 \ Inp  ut \ 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-&gt; client = $ client; 
      } 
       
       
      保护函数configure()
       {
       $ this this 
        - &gt  ; setName('freq:tweet')
        - &gt; setDescription('显示推文')
      } 
       
       
      保护函数execute(InputInterface $ input,OutputInterface $ output)
       {
       
       //检索 用户的时间线
       $ tweets = $ twitter-&gt; getTimeline(array(
      'count'=&gt;  5 
      )); 
       
       //或者使用通用查询方法检索时间线
       $ response = $ twitter-&gt; query('statuses / user_timeline','GET','json',$ parameters)  ; 
       $ tweets = json_decode($ response-&gt; getContent()); 
       
       $ output-&gt; writeln($ tweets); 
      } 
      } 
         \  n 
       

      问题是我收到'致命错误:类'源\ Commands \ TwitterCommand'在C:...中找不到',错误发送在这一行: $ application-&gt; add(new TwitterCommand($ client));

      对于我做错了什么? / p>

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doue1925
    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

    点赞 评论