Home  >  Article  >  Backend Development  >  github - How to install composer in a php file and automatically install dependent libraries?

github - How to install composer in a php file and automatically install dependent libraries?

WBOY
WBOYOriginal
2016-10-11 14:23:161393browse

We are developing a blog plug-in and need to use this project Parser. How to reference this project in the PHP file without composer, or directly install composer and this library in the PHP file?

Reply content:

We are developing a blog plug-in and need to use this project Parser. How to reference this project in the PHP file without composer, or directly install composer and this library in the PHP file?

You can just use an autoloader, but it’s better to use composer, it’s more convenient after all
https://github.com/WhichBrows...

<code>function InstallComposer()
{
    file_put_contents(__DIR__ . '/composer-setup.php', file_get_contents('https://getcomposer.org/installer'));
    shell_exec('php composer-setup.php');
}

function InstallParser(){
    shell_exec('php composer.phar require whichbrowser/parser');
}</code>

  1. If you don’t use Composer: If there are few files, just require comes in. If there are many files involved, check whether the namespace usage specification of the library is Psr4 or Psr0, and write an autoload method. To automatically import files, you can refer to ClassLoader of Composer.

  2. If you use Composer, just write a composer.json and put the required libraries into require.

    <code>#composer.json
    "require": {
        "php": ">=5.3.3",
        "godtail/db": "dev-master" #例子
    }
    
    #然后
    composer install`
    
    #引入autoload文件
    require '../vendor/autoload.php';
    </code>

Of course it is recommended to use Composer, which is componentized and more convenient.

Install parser first
and then add the file

<code>require 'vendor/autoload.php';</code>

That’s it

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn