PHP未捕获错误:使用作曲家自动加载找不到类

我正在使用 SymfonyConsole 包编写一个简单的项目,但是我遇到了 class not found 异常:

I'm writing a simple project using SymfonyConsole package, but I got class not found exception:

PHP Fatal error:  Uncaught Error: Class 'ProjectExtractLinkCommand' not found in /home/PhpstormProjects/RVLE/RVLE.php:9
Stack trace:
#0 {main}
  thrown in /home/PhpstormProjects/RVLE/RVLE.php on line 9

我找不到问题所在,有人说自动加载器不是标准的,您应该自己编写.我还更新了 composer 并运行了 composer dump-autoload.

I can't find what the problem, somebody says autoloader is not standard and you should write it your own. I also updated composer and ran composer dump-autoload.

这是我的文件 ->

RVLE.php:

#!/usr/bin/env php
<?php
require 'vendor/autoload.php';

use ProjectExtractLinkCommand;
use SymfonyComponentConsoleApplication;

$app = new Application('RVLE' , '1.0');
$app->add(new ExtractLinkCommand());
$app->run();

extractCommand.php:

<?php namespace Project;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputArgument;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;

class ExtractLinkCommand extends Command
{
    public function configure()
    {
        $this->setName('getLinks')
            ->setDescription('extract all available video links for given page url')
            ->addArgument('url', InputArgument::REQUIRED, 'page link');
    }

    public function execute(InputInterface $input, OutputInterface $output)
    {
        $url = $input->getArgument('url');    
        $output->writeln($url);
    }
}

composer.json:

{
  "require": {
    "symfony/console": "^3.3"
  },
  "autoload": {
    "psr-4": {
      "Project\": "src/"
    }
  }
}

这是我的项目结构:

.
├── composer.json
├── composer.lock
├── RVLE.php
├── src
│   └── extractCommand.php
└── vendor
    ├── autoload.php
    ├── bin
    ├── composer
    ├── psr
    └── symfony

推荐答案

我认为你需要将你的文件名与你的类名匹配,所以它应该是 ExtractLinkCommand.php,否则作曲家自动加载器赢了没找到.

I think you need to match your file name to your class name so it should be ExtractLinkCommand.php, otherwise the composer autoloader won't find it.

相关文章