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?

  • 写回答

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();

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

报告相同问题?