所以,我最近在我的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 'SwaggerClientApiMyPHPClientApi' 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): AppFrontalControllerSleepingPlaceController->showList()
#1 /vagrant/public/personal-project/vendor/symfony/http-kernel/HttpKernel.php(68): SymfonyComponentHttpKernelHttpKernel->handleRaw(Object(SymfonyComponentHttpFoundationRequest), 1)
#2 /vagrant/public/personal-project/vendor/silex/silex/src/Silex/Application.php(496): SymfonyComponentHttpKernelHttpKernel->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true)
#3 /vagrant/public/personal-project/vendor/silex/silex/src/Silex/Application.php(477): SilexApplication->handle(Object(SymfonyComponentHttpFoundationRequest))
#4 /vagrant/public/personal-project/web/index.php(10): SilexApplication->run()
#5 {main}
那我做错了什么?
我的私人包的作曲家。看起来像这样:
{
"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/subdirectory。
顺便说一句,该客户是由Swagger Editor在线工具自动生成的https://editor.swagger.io/
您在作曲家中覆盖了您的根名空间:
"autoload": {
"psr-4": { "Swagger\Client\" : "lib/" }
}
注意您的错误:Uncaught Error: Class 'SwaggerClientApiMyPHPClientApi' not found
作曲家在vendor
文件夹中安装依赖项,但您告诉它在lib
文件夹中找到SwaggerClient
名称空间。
假设您的私人回购遵循PSR-4(由于使用PSR-4自动加载器,因此可以安全地假设(,则您只需要在作曲家中删除此行并再次生成作曲家自动加载器(composer dump-autoloader
(。