Basic use of composer
Use composer. JSON in the project
To use composer in a project, you need a composer. JSON file. This file is mainly used to declare the relationship between packages and other element labels.
requireKeywords
The first thing in composer. JSON is to use the require keyword. You will tell composer which packages are required by your project.
{ "require": { "monolog/monolog": "1.0.*" }}
As you can see, the require object will map the package name (monolog/monolog) And the package version is 1. 0 .*
Package name
Basically, the package name is the Master name/Project Name (monolog/monolog), The main name must be unique, but the project, that is, the name of our package, such as igorw/JSON, and seldaek/JSON.
Package version
We need to use the monolog version 1. 0. *, which means that as long as the version is 1.0 branch, such as 1.0.0, 1.0.2 or 1.0.99
Two versions are defined:
Standard Version: defines a version package file with minimum guarantee, for example, 1.0.2.
Versions within a certain range: Use a comparison symbol to define the range of valid versions. Valid symbols include>,>=,<,<=,!=
Wildcard:Special match symbol *, for example, 1. 0. * is equivalent to> = 1.0, <1.1
Next important version :~ The best explanation of the symbol is ,~ 1.2 is equivalent to> 1.2, <2.0, ~ 1.2.3 is equivalent to> = 1.2.3 and <1.3.
Installation Package
Run in the project file path
$ composer install
In this way, he will automatically download the monolog/monolog file to your vendor directory.
The next thing to note is:
composer.lock-Lock a file
After all required packages are installed, composer generates a standard package version file in the composer. Lock file. This will lock the versions of all packages.
Use composer. Lock (together with composer. JSON of course) to control the version of your project
This is very important. When we use the install command to process it, it will first determine composer. whether the lock file exists. If yes, the corresponding version will be downloaded (not in composer. JSON configuration), which means that anyone who downloads the project will get the same version.
If no composer. Lock exists, composer will read the required package and relative version through composer. JSON, and then create the composer. Lock file.
In this way, after your package has a new version, you will not automatically update it. upgrade it to a new version and use the update command, in this way, you can get the latest version of the package and update your composer. lock file.
$ PHP composer. phar update or $ composer update
Packagist, I can learn from the code of many people, and it is more convenient !)
Packagist is the main warehouse of composer. You can check it out. The basis of the composer warehouse is the package source code. You can obtain it at will. packagist aims to build a warehouse that anyone can use, this means any require package in your file.
About automatic loading
To facilitate the loading of package files, composer automatically generates a file named "vendor/autoload. php". You can only use it wherever you need it.
require ‘vendor/autoload.php‘;
This means that you can use third-party code very conveniently. If your project needs to use monlog, you can directly use it. They have all loaded it automatically!
$log = new Monolog\Logger(‘name‘);$log->pushHandler(new Monolog\Handler\StreamHandler(‘app.log‘, Monolog\Logger::WARNING)); $log->addWarning(‘Foo‘);
Of course, you can also load your code in composer. JSON:
{ "autoload": { "psr-0": {"Acme": "src/"} }}
Composer will register the psr-0 as an Acme namespace
You can define a ing to the file directory through the namespace. the src directory is your root directory, and the vendor directory is at the same level. For example, a file is src/Acme/Foo. PHP contains the Acme \ Foo class.
After you add autoload, you must re-install it to generate the file vendor/autoload. php.
When we reference this file, we will return the strength of an autoloader class, 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:
$loader = require ‘vendor/autoload.php‘;$loader->add(‘Acme\Test‘, __DIR__);