SoFunction
Updated on 2025-04-09

What is the function of the file

Basic use of Composer

Use in a project

When using composer in the project, you need to have a file, which is mainly used to declare the relationship between packages and some other element tags.


require keywords

The first thing is to use the require keyword, you will tell composer which packages are what your project needs.

Copy the codeThe code is as follows:

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

As you can see, the required object will map the package name (monolog/monolog) and the package version is 1.0.*


Naming of the package

Basically, the name of the package is the main name/project name (monolog/monolog). The main name must be unique, but the name of the project, that is, our package can have the same name, for example: igorw/json, and seldaek/json

Package version

The version we need to use is 1.0.*, which means that as long as the version is a 1.0 branch, such as 1.0.0, 1.0.2 or 1.0.99

There are two ways to define versions:

1. Standard version: Define guaranteed version package files, such as: 1.0.2
2. Versions with a certain range: Use comparison symbols to define the range of valid versions. Valid symbols include >, >=, <,<=, !=
3. Wildcards: Special matching symbol *, for example, 1.0.* is equivalent to >=1.0, <1.1 version
4. The next important version: The best explanation for the ~ symbol is that ~1.2 is equivalent to >1.2, <2.0, but ~1.2.3 is equivalent to >=1.2.3, <1.3.

Installation package

Run under the project file path

Copy the codeThe code is as follows:

$ composer install

In this way, it will automatically download the monolog/monolog file to your vendor directory.

Next thing to explain is

- Lock the file

After installing all required packages, composer will generate a standard package version file in the file. This will lock the versions of all packages.

Use (and of course) to control the version of your project

This is very important. When we use the install command to process it first determines whether the file exists. If it exists, the corresponding version will be downloaded (not in the configuration inside), which means that anyone who downloads the project will get the same version.

If it does not exist, composer will read the required package and the relative version through it, and then create the file

In this way, you will not automatically update after your package has a new version. You can upgrade to a new version and use the update command to get the latest version of the package and update your files.

$ php update
or
$ composer update

Packagist (This should be a composer, which feels a bit like a python package. Although it is not that powerful, haha, with this standard, it will be very easy for everyone to develop a website in the future. You can learn from the code of many people, and it will be more convenient!)
Packagist is the main repository of composer. You can go and have a look. The basis of composer repository is the source code of the package, which you can obtain at will. The purpose of Packagist is to build a repository that anyone can use, which means that you can use any package in your file.

About automatic loading

To facilitate loading package files, Composer automatically generates a file vendor/, which you can use only in any place you need to use
require 'vendor/';

This means that you can use third-party code very, very conveniently. Assuming your project needs to use monlog, you can use it directly, they have been loaded automatically!

Copy the codeThe code is as follows:

$log = new Monolog\Logger('name');
$log->pushHandler(new Monolog\Handler\StreamHandler('', Monolog\Logger::WARNING));
$log->addWarning('Foo');

Of course you can also load your own code in:

Copy the codeThe code is as follows:

{
    "autoload": {
        "psr-0": {"Acme": "src/"}
    }
}

Composer will register psr-0 as Acme's namespace

You can define a mapping to the file directory through namespace. The src directory is your root directory and vendor is the directory of the same level. For example, a file is: src/Acme/, which contains the Acme\Foo class

After you add autoload, you must reinstall to generate vendor/file

When we reference this file, the strength of the autoloader class will be returned, so you can put the returned value into a variable and then add more namespaces. This is very convenient in the development environment, for example:

Copy the codeThe code is as follows:

$loader = require 'vendor/';
$loader->add('Acme\Test', __DIR__);

What is the function of the file

The install command reads the file from the current directory, processes the dependencies, and installs it in the vendor directory.

Copy the codeThe code is as follows:

composer install

If a file exists in the current directory, it will read the dependent version from this file, rather than obtaining the dependency based on the file. This ensures that every consumer of the library gets the same dependency version.

If there is no file, composer will create it after processing the dependency.

In order to get the latest version of the dependency and upgrade the file, you should use the update command.

Copy the codeThe code is as follows:

composer update

This will resolve all dependencies of the project and write the exact version number to .

If you just want to update a few packages, you can list them separately like this:

Copy the codeThe code is as follows:

composer update vendor/package vendor/package2

You can also use wildcards for batch updates:

Copy the codeThe code is as follows:

composer update vendor/*