Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to manage dependencies with Composer

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use Composer to manage dependencies, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

Composer was originally designed to manage suite dependencies in Symfony, a PHP Framework tool, but it is now an independent open source project because it is simple and easy to use. Many Framework and libraries can now be installed and managed using composer.

In fact, on PHP, there has been such a package dependency management tool for a long time, namely PEAR. However, the setting of PEAR is too complicated, and it is difficult to set individual dependencies for individual projects, so Composer is now popular with the public.

This introduction is for users only, so it won't focus on what the package developer needs to know.

* install

If you are a user of Windows, just download the installation file and perform the installation:

Https://getcomposer.org/Composer-Setup.exe

If you want to install it manually, you can refer to the guidelines on the official website:

Http://getcomposer.org/doc/00-intro.md#installation-windows

If you are a user of the UNIX Like system, you can install it through the following instructions: (you need to install curl first)

Curl-sS https://getcomposer.org/installer | php

The installer checks the settings of PHP and downloads composer.phar to the current directory. To execute composer, you can execute

Php composer.phar

Or simply change it to an executive file.

> mv composer.phar composer > chmod + x composer

Then execute. / composer.

However, if you need it in different working directories, and you have no problem with execution permissions, you can copy the files directly to / usr/local/bin.

* set dependency

When using composer in a project, you first need to generate a composer.json file that specifies the package and version to be used. For example, when you need to use phpmailer to send a letter, you can specify:

{"require": {"phpmailer/phpmailer": "~ 5.2.7"}}

Then perform the installation:

Eng-Hsu-Pingteki-MacBook-Air:2-1a fillano$ composer installLoading composer repositories with package informationInstalling dependencies (including require-dev)-Installing phpmailer/phpmailer (v5.2.7) Downloading: 100% Writing lock fileGenerating autoload filesFeng-Hsu-Pingteki-MacBook-Air:2-1a fillano$

So it's installed. Take a look at what's on the bottom:

Feng-Hsu-Pingteki-MacBook-Air:2-1a fillano$ ls-ltotal 16When RWMurray Rafael-1 fillano staff 66 10 11 18:15 composer.json-rw-r--r-- 1 fillano staff 2330 10 11 18:16 composer.lockdrwxr-xr-x 5 fillano staff 170 10 11 18:16 vendor

According to the files in the directory, you can find that there are only composer.json files, but after installation, there is an extra composer.lock file and the vendor directory. Let's first take a look at the contents of composer.lock:

Feng-Hsu-Pingteki-MacBook-Air:2-1a fillano$ cat composer.lock {"_ readme": ["This file locks the dependencies of your project to a known state", "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file"]," hash ":" 065c23f92d5ae579cb91beff67f41196 "," packages ": [{" name ":" phpmailer/phpmailer " "version": "v5.2.7", "source": {"type": "git", "url": "https://github.com/PHPMailer/PHPMailer.git"," reference ":" 8717a79565b2c0ed67f851d70e1949febdf3b226 "}," dist ": {" type ":" zip " "url": "https://api.github.com/repos/PHPMailer/PHPMailer/zipball/8717a79565b2c0ed67f851d70e1949febdf3b226"," reference ":" 8717a79565b2c0ed67f851d70e1949febdf3b226 "," shasum ":"}," require ": {" php ":" > = 5.0.0 "} "require-dev": {"phpdocumentor/phpdocumentor": "*", "phpunit/phpunit": "*"}, "type": "library", "autoload": {"classmap": ["class.phpmailer.php" "class.pop3.php", "class.smtp.php"]}, "notification-url": "https://packagist.org/downloads/"," license ": [" LGPL-2.1 "]," authors ": [.... The following outline

It looks like information about just installing the package.

Take another look at what is in the vendor directory:

Feng-Hsu-Pingteki-MacBook-Air:2-1a fillano$ tree vendorvendor ├── autoload.php ├── composer │ ├── ClassLoader.php │ ├── autoload_classmap.php │ ├── autoload_namespaces.php │ ├── autoload_real.php │ └── installed.json └── phpmailer └── phpmailer ├── LICENSE ├── PHPMailerAutoload.php ├── README.md ├ ── changelog.md ├── class.phpmailer.php ├── class.pop3.php ├── class.smtp.php ├── composer.json ├── docs │ ├── Callback_function_notes.txt │ ├── DomainKeys_notes.txt │ ├── Note_for_SMTP_debugging.txt │ ├── Extending.html │ ├── faq.html │ ├── generatedocs.sh │ └── pop3_article.txt... The following outline

It seems that in addition to installing phpmailer in the phpmailer directory, there are mainly autoload.php files and composer directories.

It turns out that to load a package installed through composer, you need to reference the vendor/autoload.php file first, and then you can use phpmailer. Write a simple program to test it:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report