2017-08-24 73 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