简体   繁体   中英

PHP Uncaught Error: Class not found using composer autoload

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

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

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 .

Here are my files ->

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/"
    }
  }
}

This is my project structure:

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

我认为您需要将文件名与类名进行ExtractLinkCommand.php ,因此它应该是ExtractLinkCommand.php ,否则作曲家自动加载器将找不到它。

PSR-4 only works with namespaces. It removed the namespace prefix given in composer.json from the full class name, and the remainder is converted into a path, ".php" added at the end, and searched in the path given. A class myNamespace\\myClass and "psr-4":{"myNamespace\\": "src"} will try to load src/myClass.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM