Easily learn to use composer

 Watson Blog December 13, 2017 12:00:29 PHP technology comment two hundred and fifty-five Reading mode

brief introduction

For modern languages, the package manager is basically a standard configuration. Java has Maven, Python has pip, NodeJs has npm, and PHP used to be PERAR, but PEAR has many shortcomings.

  • Dependent processing is prone to problems
  • Very complex configuration
  • The hard to use command line interface was born in Composer. Composer is a tool used to manage dependencies in PHP. You can declare the dependent PHP packages in your own project, and Composer will help us install these dependent library files. This is similar to the yum command in CentOs.

It's no exaggeration to say that Composer's PHP is not a good one.

Composer function

  1. Composer is used to manage (update, download, uninstall) php packages
  2. Composer can also realize automatic loading

Steps for installing composer in Linux

a. Set CentOs time to synchronize with the network time (you need to set if your server time is inaccurate)

  1. Install ntpdate tool
     $ yum -y install ntp ntpdate
  2. Set system time to synchronize with network time
     $ ntpdate cn.pool.ntp.org
  3. Write system time to hardware time
     $ hwclock --systohc

b. Centos Global Installation Composer

 //Composer download address https://getcomposer.org/composer.phar //Move the downloaded composer.phar file to the/usr/local/bin directory to make the command available globally mv composer.phar /usr/local/bin/composer

c. Set composer source

If the composer source is not set, the composer will download packages from abroad by default, which will be very slow.

 $ composer config -g repo.packagist composer  https://packagist.phpcomposer.com

Use Composer to install dependent packages

Create a composer file in the project directory to indicate the dependency, such as your project dependency on Monolog

Then you need to enter the following contents in the composer.json file:

 { "require":{ "monolog/monolog":"1.21.*" } }

Installation dependency

  • Installation dependency is very simple, just run it in the project directory:
     $ composer install

After running composer install, the package written in the composer. json file will be downloaded

  • If the composer is not installed globally, run
     $ php composer.phar install

Note that PHP must have added environment variables

  • Another way to download packages using composer
     $composer install package name

Code installed using Composer

Composer provides the feature of automatic loading. Just add the following line to your code:

 require 'vendor/autoload.php';

Composer warehouse

The packagist.org website is the repository of Composer, where many famous PHP libraries can be found. We can also submit our own library.

Update Composer

Composer should be updated frequently

 $ composer self-update

Update Dependencies

//If you have modified the Composer.json file, you need to execute the following command to make the Composer.json file take effect

 $ composer update

If you just want to update several packages, you can list them separately:

 $ composer update vendor/package vendor/package2

How to use the code we wrote

a. Write composer. json to tell composer where our code is

 "autoload":{ "psr-4":{ "app\\Controller\\":"app/Controller", "app\\Model\\":"app/Model" } } "app\\Controller\\":"app/Controller", The namespace is on the left ("app   Controller  ") On the right ("app/Controller") is the code directory of the corresponding namespace

b. Update the composer. json file

 $ composer update

c. As long as in the entry file

 //You can also use your own code require('./ vendor/autoload.php'); <

Complete steps for using composer for the first time

a. Go to the official website and select manual download to get the composer.phar file

 https://getcomposer.org/composer.phar

b. Put composer into Linux command

 $ mv composer.phar /usr/local/bin/composer

c. Generate a composer. json file

 composer init

d. Write the package you want to download into the composer. json file. Bao Ming https://packagist.org/ Website search

 { "require":{ "monolog/monolog":"1.21.*", "phpoffice/phpexcel": "^1.8", "overtrue/wechat": "~3.1" } }

e. Configure packagist domestic image

 $ composer config -g repo.packagist composer  https://packagist.phpcomposer.com

f. Installation package

 $ composer install

How to install the package after composer.json has been generated

a. Make sure you want to install the package name first

For example, I want to download predis/redis

b. Run the following command to download

 $ composer require predis/predis

Original link: https://www.3maio.com/w-detail/17

 Watson Blog
  • This article is written by Published on December 13, 2017 12:00:29
  • This article is collected and sorted by the website of Mutual Benefit, and the email address for problem feedback is: wosnnet@foxmail.com , please keep the link of this article for reprinting: https://wosn.net/927.html

Comment