Composer不会自动加载我的自动加载器类

时间:2018-02-07 17:18:46

标签: php composer-php swagger autoload

所以,我最近在我的composer.json文件中包含了一个私有包,并且工作正常。它按我的意愿克隆了我的BitBucket存储库的项目。但是,我遇到了自动加载程序包类的问题。

如果我在我的Silex项目的控制器中手动添加自动加载器,它可以很好地工作。

require_once('/var/www/vendor/my-vendor/my-private-package/autoload.php');

但是,如果我没有添加autoload.php类,那么我会收到类错误:

18/02/07 17:05:33 [error] 1174#0: *1187 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Class 'Swagger\Client\Api\MyPHPClientApi' not found in /vagrant/public/personal-project/src/App/Frontal/Controller/MyController.php:88
Stack trace:
#0 /vagrant/public/personal-project/vendor/symfony/http-kernel/HttpKernel.php(151): App\Frontal\Controller\SleepingPlaceController->showList()
#1 /vagrant/public/personal-project/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#2 /vagrant/public/personal-project/vendor/silex/silex/src/Silex/Application.php(496): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#3 /vagrant/public/personal-project/vendor/silex/silex/src/Silex/Application.php(477): Silex\Application->handle(Object(Symfony\Component\HttpFoundation\Request))
#4 /vagrant/public/personal-project/web/index.php(10): Silex\Application->run()
#5 {main}

那么我做错了什么?

我的私有包的composer.json看起来像这样:

{
    "name": "my-vendor/my-private-package",
    "description": "",
    "keywords": [
        "swagger",
        "php",
        "sdk",
        "api"
    ],
    "homepage": "http://swagger.io",
    "license": "proprietary",
    "authors": [
        {
            "name": "Swagger and contributors",
            "homepage": "https://github.com/swagger-api/swagger-codegen"
        }
    ],
    "require": {
        "php": ">=5.4",
        "ext-curl": "*",
        "ext-json": "*",
        "ext-mbstring": "*"
    },
    "require-dev": {
        "phpunit/phpunit": "~4.8",
        "satooshi/php-coveralls": "~1.0",
        "squizlabs/php_codesniffer": "~2.6",
        "friendsofphp/php-cs-fixer": "~1.12"
    },
    "autoload": {
        "psr-4": { "Swagger\\Client\\" : "lib/" }
    },
    "autoload-dev": {
        "psr-4": { "Swagger\\Client\\" : "test/" }
    }
}

私有包中的类位于lib /子目录下。

顺便说一下,这个客户端是由Swagger编辑器在线工具https://editor.swagger.io/自动生成的

1 个答案:

答案 0 :(得分:1)

您在此处覆盖作曲家的根名称空间:

"autoload": {
  "psr-4": { "Swagger\\Client\\" : "lib/" }
}

请注意您的错误:Uncaught Error: Class 'Swagger\Client\Api\MyPHPClientApi' not found

Composer会在vendor文件夹中安装您的依赖项,但是您要告诉它在Swagger\Client\文件夹中找到lib命名空间。

假设您的私人仓库遵循PSR-4(由于您正在使用PSR-4自动加载器,因此可以安全地假设),您只需要在作曲家中移除此行并generate again the composer autoloader({ {1}})。