2017-08-24 69 views
2

我正在写使用SymfonyConsole包一个简单的项目发现,但我得到了类未发现异常:PHP未捕获的错误:类不使用自动加载作曲家

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

我找不到什么问题,有人说自动加载器不是标准的,你应该自己写。 我也更新了作曲家,并跑了composer dump-autoload

这里是我的文件 - >

RVLE.php

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

use Project\ExtractLinkCommand; 
use Symfony\Component\Console\Application; 

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

extractCommand.php

<?php namespace Project; 
use Symfony\Component\Console\Command\Command; 
use Symfony\Component\Console\Input\InputArgument; 
use Symfony\Component\Console\Input\InputInterface; 
use Symfony\Component\Console\Output\OutputInterface; 

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 

回答

4

我想你需要你的文件名匹配的类名,所以应该是ExtractLinkCommand.php,否则作曲家自动加载磁带机将无法找到它。

+2

接受此答案如何?毕竟,它应该已经解决了你的问题! – localheinz

1

PSR-4仅适用于名称空间。它从完整的类名称中删除了composer.json中给出的名称空间前缀,其余部分将转换为路径,最后添加“.php”,并在给定的路径中进行搜索。类myNamespace \ myClass和“psr-4”:{“myNamespace \”:“src”}将尝试加载src/myClass.php