dougu6815
2015-02-10 22:09
浏览 78
已采纳

使用Composer的自动加载器和个人代码

I'm building a small project using Composer, but I now have to use some custom code, in the parent folder of vendor folder.

Similar file structure: libraries > companyname > namespace > classfile.php

Is it possible to effectively use the composer autoloader? It seems that it is, but I'm having trouble wrapping my head around it.

Would it be easier to use a second autoloader script?

图片转代码服务由CSDN问答提供 功能建议

我正在使用Composer构建一个小项目,但我现在必须在父文件夹中使用一些自定义代码 供应商文件夹。

类似的文件结构: libraries > companyname > 名称空间> classfile.php

是否可以有效地使用composer自动加载器? 它似乎是,但我无法绕过它。

使用第二个自动加载器脚本会更容易吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douraoyw194498 2015-02-10 22:18
    已采纳

    We're loading in our own code via composer.

    Our code is installed in the lib folder under our company name. Our composer file looks like this.

    {
        "config": {
            "vendor-dir": "lib"
        },
        "require": {
            "twig/twig": "v1.15.1",
            "symfony/symfony": "2.5.4"
        },
        "autoload": {
            "psr-4": {
                "CompanyName\\": "lib/companyName/src"
            }
        }
    }
    

    the autoload psr4 section is the important part. CompanyName will resolve files located in lib/companyName/src.

    Inside lib/companyName/src, you'd have a file called ThingDoer.php

    <?php
    
    namespace CompanyName;
    
    class ThingDoer {
        public static function doThings() {}
    }
    

    And now from anywhere in your codebase, you can call CompanyName\ThingDoer::doThings();

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题