简体   繁体   English

在为Composer创建包时,如何加载依赖项?

[英]When creating a package for Composer, how do you load dependencies?

I've made packages for NPM and I know the drill there. 我已经为NPM制作了包装,我知道那里的钻头。 You simply list your dependencies and then require() them. 您只需列出您的依赖项,然后require()它们。 Node searches node_modules , finds the package, loads it and you're good to go. 节点搜索node_modules ,找到包,加载它,你就可以了。

How does that work in PHP, though? 但是,这在PHP中是如何工作的? My dependency is PoParser . 我的依赖是PoParser I know I can just do: 我知道我可以这样做:

require __DIR__ . '/vendor/autoload.php';

But when I publish my package, I wouldn't publish the third party dependency code, right? 但是当我发布我的包时,我不会发布第三方依赖代码,对吧? My package should only contain my code. 我的包应该只包含我的代码。 At least that's the case with NPM. 至少就是NPM的情况。

So if I don't have that vendor folder when I publish my thing. 所以如果我发布我的东西时没有那个vendor文件夹。 How would I require my dependency? 我如何要求我的依赖? In Node, require() does that hard work. 在Node中, require()完成这项工作。 How would I do it here? 我怎么在这里做?

Here's my folder structure, if that helps: 这是我的文件夹结构,如果有帮助:

vendor
    /composer
        ...
    /sepia
        /po-parser
            ...
            /src
                /Sepia
                ...
                // dependency classes
                ...
            ...
    autoload.php
composer.json
composer.lock
MyThing.class.php
Other.class.php

Composer will handle that for you. Composer将为您处理。 Put your dependencies inside the composer.json file and when you require your project it will download the dependencies too. 将您的依赖项放在composer.json文件中,当您需要项目时,它也会下载依赖项。

Take a look at this tutorial: https://dev.to/ahmedkhan/smart-guide-on-creating-a-complete-php-package-using-composer 看看这个教程: https//dev.to/ahmedkhan/smart-guide-on-creating-a-complete-php-package-using-composer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM