Composer PSR-4自动加载完全被忽略

时间:2019-02-11 13:05:29

标签: php composer-php autoloader psr-4

我有composer.json文件:

{
    "name": "marko/art-empire",
    "description": "Social network",
    "type": "project",
    "authors": [
        {
            "name": "Marko Ilic",
            "email": "markowebdeveloper@gmail.com"
        }
    ],
    "require": {},
    "autoload": {
      "psr-4": {
        "Songs\\": "songs/"
      }
    }
}

autoload_psr4.php文件:

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'Songs\\' => array($baseDir . '/songs'),
);

歌曲文件夹(位于根目录中)中的RandomSong.php文件:

namespace Song;

class RandomSong
{
    public function songName()
    {
        return 'Random Song';
    }
}

test.php文件:

require 'vendor/autoload.php';

use Songs\RandomSong;

$randomSong = new RandomSong();
echo $randomSong->songName();

如您所见,我正在尝试自动加载RandomSong类,但我一直在获取:

Fatal error: Uncaught Error: Class 'Songs\RandomSong' not found in test.php

请帮助,谢谢。

2 个答案:

答案 0 :(得分:0)

您的命名空间称为{"error":{"code":"UnsupportedApiVersion","message":"The HTTP resource that matches the request URI 'http://localhost:27713/api/v1.0/vt' does not support the API version '1.0'.","innerError":null}} ,但您将其称为Song

答案 1 :(得分:0)

您的RandomSong使用Song命名空间,而您的加载程序用于Songs命名空间。