For more information about composer, see Table Of Contents.
Composer is not a package manager. Yes, it involves "packages" and "libraries", but it is managed based on each project and installed in a directory of your project (such as vendor. By default, it does not install anything globally. Therefore, this is just dependency management.
This idea is not new, and Composer is strongly inspired by node's npm and ruby's bundler. At that time, there was no similar tool in PHP.
Composer will solve the problem for you as follows:
A) you have a project dependent on several databases.
B) some of these libraries depend on other libraries.
C) you declare what you are dependent on.
D) Composer will find out which version of the packages need to be installed and install them (download them to your project ).
Because laravel is managed by composer, everything is based on laravel.
Download and install composer
In fact, I have already written about laravel5.1 installation experience.
Here we add:
1. for some reason, the access to the foreign composer Resource website is slow, resulting in frequent connection timeout errors during the composer install or update operation.
"Global: composer config-g repo. packagist composer https://packagist.phpcomposer.com local project (to be executed under the current directory of the project): composer config repo. packagist composer https://packagist.phpcomposer.com "after the command is executed. this section is added to the json file, which indicates that the Chinese image is successfully added, this image "" repositories ": {" packagist ": {" type ": "composer", "url": "https://packagist.phpcomposer.com "}}"
Use composer selfupdate to update the version of the composer tool.
About the composer. json file
{"Name": "laravel/laravel", "description": "The Laravel Framework. "," keywords ": [" framework "," laravel "]," license ":" MIT "," type ":" project "," require ": {// Here is the project that must be installed by composer, which is equivalent to the production environment "php": ">=5.5.9", "laravel/framework": "5. 2. * ", // require requires a package name. this is the package name" laravelcollective/html ":" 5. 2. * "," yuanchao/laravel-5-markdown-editor ":" dev-master "}," require-dev ": {// This is the project that needs to be installed for development, equivalent to the development environment, you can use-no -Dev to cancel the package "fzaninotto/faker ":"~ 1.4 "," mockery/mockery ":" 0. 9. * "," phpunit/phpunit ":"~ 4.0 "," symfony/css-selector ":" 2. 8. * | 3. 0. * "," symfony/dom-crawler ":" 2. 8. * | 3. 0. * "}," autoload ": {" classmap ": [" database "]," psr-4 ": {" App \ ":" app /"}}, "autoload-dev": {"classmap": ["tests/TestCase. php "]}," scripts ": {" post-root-package-install ": [" php-r \ "copy ('. env. example ','. env '); \ ""], "post-create-project-cmd": ["php artisan key: generate"], "post-install-cmd ": ["Illuminate \ Foundation \ ComposerScripts: postInstall", "php artisan optimize"], "post-update-cmd": ["Illuminate \ Foundation \ ComposerScripts :: postUpdate "," php artisan optimize "]}," config ": {" preferred-install ":" dist "}}
Version of the package name
Name instance description
Exact version ______ 1.0.2 ______ you can specify the exact version of the package.
The range is ______> = 1.0> = 1.0, <2.0> = 1.0, <1.1 |> = 1.2 ______. you can use the comparison operator to specify a valid version range. Valid operators:>,> =, <, <=, and ,! =. You can define multiple ranges separated by commas (,), which is regarded as a logic AND processing. One pipeline symbol | it will be processed as logic OR. AND has a higher priority than OR.
Wildcard ______ 1. 0. * ______ you can use wildcard to specify a mode. 1. 0. and> = 1.0, <1.1 is equivalent.
Assignment operator ______~ 1.2 ______ this is very useful for projects that follow the semantic version number .~ 1.2 is equivalent to> = 1.2, <2.0.
We need to focus on wildcards and tildes. wildcards are easy to understand. tildes are a bit open-ended ,~ It is best to use an example to explain :~ 1.2 is equivalent to> = 1.2, <2.0 (mark the lowest version on which you are dependent), and ~ 1.2.3 is equivalent to> = 1.2.3, <1.3. (Specify the lowest version, but allow the last digit of the version to rise .) 'Semantic is hard to understand, but you can know how to use it directly by looking at the example.
Basic usage
Composer installs packages by reading the composer. json and composer. lock files.
After the dependency is installed, Composer writes the exact version number list during installation to the composer. lock file. This will lock the specific version of the modified project. The install command checks whether the lock file exists. If yes, it downloads the specified version (ignoring the definition in the composer. json file ). If the composer. lock file does not exist, Composer reads composer. json and creates the lock file.
Common usage:
1. composer install (the install command reads the composer. json file from the current directory, processes the dependency, and installs it to the vendor directory .)
2. composer install XXXX (this is used when some packages are installed separately)
3. composer update (to obtain the latest dependent version and upgrade the composer. lock file ,)
4. composer update XXX (similar)
-- Prefer-source: Two download methods are available: source and dist. For stable versions, composer uses the dist method by default. Source indicates the version control source. If -- prefer-source is enabled, composer will install from source (if any ). This is useful if you want to use a bugfix to your project. In addition, you can directly obtain the dependency from the local version Library. -- Prefer-dist: Unlike -- prefer-source, composer will obtain the dist as much as possible, which will greatly accelerate the installation on build servers. This is also a way to avoid git problems, if you do not know how to set correctly. -- Dry-run: If you only want to demonstrate and not install a package, you can run the -- dry-run command to simulate installation and display what will happen. -- Dev: install the packages listed in The require-dev field (this is a default value ). -- No-dev: Skip the packages listed in The require-dev field. -- No-scripts: Skip the script defined in the composer. json file. -- No-plugins: disable plugins. -- No-progress: remove the progress information, which can avoid chaotic display of terminals or scripts that do not process line breaks. -- Optimize-autoloader (-o): converts PSR-0/4 autoloading to classmap for faster loading support. This is especially recommended in the production environment, but it does not take the default value because the running takes some time.
4. composer require (add a new dependency package to the composer. json file in the current directory by using the require command. But it cannot be updated.) 5. composer dump-autoload (in some cases, you need to update autoloader. for example, you have added a new class to your package .)
Automatic loading
The automatic loading of composer will produce the file vendor/autoload. php, and then call this file to obtain the automatic loading of classes in the file.
Automatic loading supports only PSR-4 and PSR-0 naming
Under the psr-4 key you define a mapping from namespaces to paths, relative to the package root. {"autoload": {"psr-4": {"Monolog \": "src/", // The representation is similar, but the presentation meaning is not the same, psr4 sets a namespace as the root directory of the package. for example, this line means that the src/directory maps to the Monolog \ root directory, then write Monolog \ Bar \ Baz when calling this package. In fact, the src/Bar/Baz will be automatically loaded here. find the class file in php and load "Vendor \ Namespace \\": "" }}} under the psr-0 key you defined a namespace ing to the actual path (relative to the package's root directory) {"autoload": {"psr-0 ": {"Monolog \": "src/", // This indicates that the src/directory is mapped to Monolog \. if you want to call Monolog \ Bar \ Baz, then the automatic loading will go to src/Monolog/Bar/Baz. php, and then load "Vendor \ Namespace \": "src/", "Vendor_Namespace _": "src /"}}}
Laravel automatically loads some more things.
Vendor/autoload. php
Reference:
1. http://docs.phpcomposer.com/00-intro.html
2. https://getcomposer.org/doc/00-intro.md