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 install php-composer

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

Share

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

This article mainly introduces how to install php-composer, which is very detailed and has a certain reference value. Friends who are interested must finish reading it!

Installation and use of php-composer (simplified version) 1. Brief introduction

Composer is a dependency management tool for PHP. It allows you to declare the code base on which the project depends, and it will install them for you in your project.

two。 System requirements

PHP version 5.3.2 + or above is required to run Composer.

Composer is multi-platform and can run on Windows, Linux, and OSX platforms at the same time.

3. Install (ubuntu) curl-sS https://getcomposer.org/installer | php mv composer.pharmv composer.phar / usr/local/bin/composer Note: if curl is not installed, you can install apt-get updateapt-get install curl with the following command

When your Composer is installed, you can use the following command to see if the installation is successful.

Composer-v Note if the above method fails for some reason, you can also download the installer through php: php-r "readfile ('https://getcomposer.org/installer');" | php

This will check some PHP settings and then download composer.phar to your working directory. This is the binary file for Composer. This is a PHAR package (PHP archive), which is the PHP archive format to help users perform some operations on the command line.

You can specify the installation directory of Composer with the-- install-dir option (it can be an absolute or relative path)

4. Use

To start using Composer in your project, you only need a composer.json file. This file contains project dependencies and other metadata.

First create a composer.json file and write the appropriate package name and version number, as shown in

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

This is followed by writing a dependency package and then installing the dependency package. Get the definition that depends on your local project, and then run the install command using Composer in your project directory (that is, the directory where composer.json is located).

Composer install

Of course, if you are on a Windows system, you can also install the dependency package by calling the composer.phar package.

Php composer.phar install

Execute composer install and enter the automatic installation. After the installation is completed, a composer.lock file with a specific version name will be generated, which needs to be submitted to version management together with composer.json.

Finally, when you need to update the dependency package, you can use the following command

Composer update

If you only want to update partial dependencies

Composer update monolog/monolog5. Automatic loading

For the automatic loading information of the library, Composer generates a vendor/autoload.php file. You can introduce it in the entry file of your project

This makes it easy for you to use third-party code. For example, if your project depends on monolog, you can start using the library like this, and they will be loaded automatically.

6.Packagist / Composer China full Mirror

Due to wall problems, foreign images of Composer often fail to work properly with install, so it is recommended to use domestic images as follows

There are two ways to enable this mirror service:

System global configuration: add the configuration information to the global configuration file config.json of Composer. For details, see "method one"

Add configuration information to the composer.json file of a project. For details, see "method 2"

Method 1: modify the global configuration file of composer

Open a command line window (windows user) or console (Linux, Mac user) and execute the following command:

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

Method 2: modify the composer.json configuration file of the current project:

Open a command line window (windows user) or console (Linux, Mac user), go to the root directory of your project (that is, the directory where the composer.json file is located), and execute the following command:

Composer config repo.packagist composer https://packagist.phpcomposer.com

The above command will automatically add the configuration information of the image at the end of the composer.json file in the current project (you can also add it manually):

"repositories": {"packagist": {"type": "composer", "url": "https://packagist.phpcomposer.com"}} 7. Use autoload in Composer to load namespaces automatically

Composer can not only help you install the dependent packages you need, but also realize the function of automatically loading namespaces. When the function libraries and class libraries we write need to be loaded automatically, we can achieve them through composer.json. It is similar to spl_autoload_register () in php, but if you look at the source code in Composer, you will see that its automatic loading function also uses the function spl_autoload_register (). "for details, please see the detailed introduction of this article."

Let's add the following code to composer.json:

{"autoload": {"psr-4": {"Test\\": "test/", "Testtwo\": "testtwo/"}

This configuration file has an autoload section, in which there is a "PSR-4", psr-4 is a class library based on psr-4 rules to automatically load the corresponding relationship, as long as the subsequent object, in the "namespace": "path" way to write their own class library information modification is completed, after the execution of the following command, you can complete the automatic loading.

Composer dumpautoload Note: the "test/" path in "psr-4": {"Test\\": "test/"} is relative to composer.json.

At this point, you can call the function library or class library written by yourself.

Note: the content of this article refers to "Composer Chinese website", and other practical features of Composer will be updated later. these are all the contents of this article "how to install php-composer". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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