duanfei8149 2017-05-05 11:02
浏览 326
已采纳

如何使用composer autoloader自动加载我的类?

By considering this files structure

| -- src /
        | -- vendor
        | -- models
        | -- libraries
        | -- ...
        | -- composer.json

How can I autoload classes located in 'models' & 'libraries' directories ?

  • 写回答

2条回答 默认 最新

  • dongtao1262 2017-05-05 11:21
    关注

    You can add your own autoloader rules into the composer.json file in your project - this adds your own rules to the vendor/autoload.php file so that your own classes will load as well as the ones in the vendor/ directory. There's more information in the docs here: https://getcomposer.org/doc/04-schema.md#autoload. Take a look at the PSR-0 section to load classes from your two directories.

    Try something like:

    {
        "autoload": {
            "psr-0": { "": ["models/", "libraries"] }
        }
    }
    

    If your classes are namespaced, then specify the namespaces as this will stop composer from looking for all classes in those directories.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言爬虫的时候元素和园代码不一样怎么解决呀
  • ¥15 VS2022多项目启动有问题
  • ¥15 SQL删除添加数据后序号不连续问题。
  • ¥15 首次运行OmniEvent运行报错
  • ¥15 有没有人知道这个问题怎么解决
  • ¥15 comsol电力电缆载流量仿真
  • ¥15 webSocket可以接TCP socket接口吗
  • ¥60 mpi并行出错,CFD++计算
  • ¥15 c#:vsto,powerpoint的外接程序中换主题颜色
  • ¥15 状态机/汽车转向灯/Sateflow