Table of Contents
After your Composer is installed, you can use the following Command to see if the installation is successful
file. This file contains the project's dependencies and other metadata.
file. You can introduce it in the entry file of your project
, so it is recommended to use domestic ones Mirroring is used as follows
spl_autoload_register()
Home Development Tools composer About the installation and use of php-composer (simplified version)

About the installation and use of php-composer (simplified version)

Mar 17, 2021 pm 05:39 PM
composer linux php

The following tutorial column will introduce you to the installation and use of php-composer (simplified version). I hope it will be helpful to friends in need!

How to install and use php-composer (simplified version)About the installation and use of php-composer (simplified version)

Composer is a dependency management tool for PHP. It allows you to declare code libraries that your project depends on and it will install them for you in your project.
《Composer Chinese website》

2. System requirements


PHP 5.3.2 or above is required to run Composer.

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

3. Installation (ubuntu)

curl -sS https://getcomposer.org/installer | php mv composer.phar
mv composer.phar /usr/local/bin/composer
Copy after login

Note: If curl is not installed, you can install it through the following command
apt-get update
apt-get install curl
Copy after login

After your Composer is installed, you can use the following Command to see if the installation is successful

composer -v
Copy after login
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
Copy after login

This will check some PHP settings and then download c

omposer.phar
to your working directory. This is the Composer binary. This is a PHAR package (PHP Archive), which is a PHP archive format that helps users perform some operations on the command line.

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

4. Use To start using Composer in your projects, you only need a

composer.json

file. This file contains the project's dependencies and other metadata.

First create a composer.json file, write the corresponding package name and version number, such as

{    
    "require": {
        "monolog/monolog": "1.13.*"
    }
}
Copy after login

After this, a dependent package is written, and then installed Dependency package. Get the defined dependencies to your local project, and then use Composer to run the install command in your project directory (that is, the directory where

composer.json

is located). <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">composer install</pre><div class="contentsignin">Copy after login</div></div>Of course, if you are in a Windows system, you can also install dependent packages by calling the

composer.phar

package. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">php composer.phar install</pre><div class="contentsignin">Copy after login</div></div>Execute composer install to enter the automatic installation. After the installation is completed, a

composer.lock

file will be generated, which contains a specific version number. This file is required. Submit it to version management together with composer.json. Finally, when you need to update dependency packages, you can use the following command<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">composer update</pre><div class="contentsignin">Copy after login</div></div>If you only want to update some dependencies

composer update monolog/monolog
Copy after login

5. Automatic loading

For library autoloading information, Composer generates a

vendor/autoload.php

file. You can introduce it in the entry file of your project

<?php
require __DIR__ . &#39;/vendor/autoload.php&#39;;
?>
Copy after login

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 automatically loaded. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;?php require __DIR__ . &amp;#39;/vendor/autoload.php&amp;#39;; $log = new Monolog\Logger(&amp;#39;name&amp;#39;); $log-&gt;pushHandler(new Monolog\Handler\StreamHandler('app.log', Monolog\Logger::WARNING)); $log-&gt;addWarning('Foo'); ?&gt;</pre><div class="contentsignin">Copy after login</div></div>6.Packagist/Composer China Full Image

Due to wall problems, foreign images of Composer often cannot be installed normally

install

There are two ways to enable this mirroring service:

System global configuration: That is, add the configuration information to Composer's global configuration file config.json. For details, see "Method 1"

Add configuration information to the composer.json file of a project. For details, see "Method 2"
  • Method 1: Modify composer's global configuration file
  • Open the command line window (windows users) or console (Linux, Mac users) and execute the following command:
composer config -g repo.packagist composer https://packagist.phpcomposer.com
Copy after login
Method 2: Modify the

composer.json
configuration file of the current project:

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

composer config repo.packagist composer https://packagist.phpcomposer.com
Copy after login

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

"repositories": {
    "packagist": {
        "type": "composer",
        "url": "https://packagist.phpcomposer.com"
    }
}
Copy after login
7. Use autoload in Composer to automatically load the namespace

Composer can not only help you In addition to installing the required dependency packages, you can also implement the function of automatically loading the namespace. When the function libraries and class libraries we write ourselves need to be automatically loaded, we can achieve this through composer.json

. It is similar to

spl_autoload_register()

in php. In fact, if you look at the source code in Composer, you will see that its automatic loading function also uses the

spl_autoload_register() function. . "For details, please see this article for detailed introduction" We add the following code in composer.json:

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

这个配置文件中有一个 autoload 段,其中有个 《PSR-4》,psr-4 是一个基于 psr-4 规则的类库自动加载对应关系,只要在其后的对象中,以 ”命名空间“: “路径” 的方式写入自己的类库信息修改完成后,之后,在执行下列命令,即可完成自动加载。

composer dumpautoload
Copy after login
注: "psr-4": {"Test\\": "test/"} 中的 "test/" 路径为相对于  composer.json 的路径

这个时候,你就可以调用你自己编写的函数库或者类库了

<?php
require __DIR__ . &#39;/vendor/autoload.php&#39;;

$testClass = new \Test\Testclass();
?>
Copy after login
注:本文内容参考了《Composer 中文网》,后续还会更新 Composer 其它的实用功能

The above is the detailed content of About the installation and use of php-composer (simplified version). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

Four ways to implement multithreading in C language Four ways to implement multithreading in C language Apr 03, 2025 pm 03:00 PM

Multithreading in the language can greatly improve program efficiency. There are four main ways to implement multithreading in C language: Create independent processes: Create multiple independently running processes, each process has its own memory space. Pseudo-multithreading: Create multiple execution streams in a process that share the same memory space and execute alternately. Multi-threaded library: Use multi-threaded libraries such as pthreads to create and manage threads, providing rich thread operation functions. Coroutine: A lightweight multi-threaded implementation that divides tasks into small subtasks and executes them in turn.

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

How to open web.xml How to open web.xml Apr 03, 2025 am 06:51 AM

To open a web.xml file, you can use the following methods: Use a text editor (such as Notepad or TextEdit) to edit commands using an integrated development environment (such as Eclipse or NetBeans) (Windows: notepad web.xml; Mac/Linux: open -a TextEdit web.xml)

Describe the purpose and usage of the ... (splat) operator in PHP function arguments and array unpacking. Describe the purpose and usage of the ... (splat) operator in PHP function arguments and array unpacking. Apr 06, 2025 am 12:07 AM

The... (splat) operator in PHP is used to unpack function parameters and arrays, improving code simplicity and efficiency. 1) Function parameter unpacking: Pass the array element as a parameter to the function. 2) Array unpacking: Unpack an array into another array or as a function parameter.

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

See all articles