duangang2825 2015-04-29 18:29
浏览 56
已采纳

如果在存储库中设置,则Composer自动加载文件不起作用

i wrote the followings

"repositories": [{
  "type": "package",
  "package": {
    "name": "rysas/ffxiv-lodestone-php-api",
    "version": "dev-master",
    "source": {
      "url": "git://github.com/rysas/Final-Fantasy-XIV-Lodestone-PHP-API.git",
      "type": "git",
      "reference": "origin/master"
    },
    "autoload": {
      "files": [
        "vendor/rysas/ffxiv-lodestone-php-api/ffxiv-lodestone-api.php"
      ]
    }
  }
}]

After doing a composer update, my library isn't loaded, everything works fine if i do the following

"repositories": [{
  "type": "package",
  "package": {
    "name": "rysas/ffxiv-lodestone-php-api",
    "version": "dev-master",
    "source": {
      "url": "git://github.com/rysas/Final-Fantasy-XIV-Lodestone-PHP-API.git",
      "type": "git",
      "reference": "origin/master"
    }
  }
}],
"autoload": {
    "psr-4": {
    "App\\": "src"
  },
  "files": [
    "vendor/rysas/ffxiv-lodestone-php-api/ffxiv-lodestone-api.php"
  ]
}

Do i really need to declare the autoloader ouside of the declaration of my package ?

  • 写回答

1条回答 默认 最新

  • doulongsha5478 2015-04-29 19:51
    关注

    When you add package information as "type=package", you have to do that from the perspective of that package.

    And the autoloading rules say that autoloading must be defined relative from the PACKAGE ROOT. The package root is the directory that gets placed at "vendor/vendorname/packagename/", i.e. you must not add either the directory "vendor", "rysas" and "ffxiv-lodestone-php-api" to the autoload definition path.

    Additionally, you should not use the "files" autoloader if the code that needs to get autoloaded is in fact a PHP class. Use the classmap autoloader for that (you can point it at distinct files as well as directories). The difference: Classmap autoloading takes place only if the class is needed. Files autoloading isn't really automatic - the files is ALWAYS executed when you require "vendor/autoload.php".

    Files autoloading is meant to add global functions outside of classes because PHP cannot autoload them. The better solution for such code would be to put them into a class as a static function - this can be autoloaded. It should be seen as a workaround for old legacy code to make it usable with Composer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧