Five tips for PHPer to learn about Composer. phpercomposer_PHP tutorial

Source: Internet
Author: User
Tags composer install
PHPer requires five tips on Composer, phpercomposer. PHPer requires five tips on Composer. phpercomposerComposer is a new generation of PHP dependency management tools. Its introduction and basic usage can be seen in the article five tips for PHP dependency management (dependency)-related PHPer, namely, phpercomposer

Composer is a new generation of PHP dependency management tools. Its introduction and basic usage can be found in this article "install and use the dependency management tool Composer in PHP". This article describes five tips for using Composer, hoping to facilitate PHP development.

1. update only a single database

You only want to update a specific library and do not want to update all its dependencies. this is simple:

composer update foo/bar

In addition, this technique can be used to solve the "warning information problem ". You must have seen the following warning information:

Warning: The lock file is not up to date with the latest changes in composer.json, you may be getting outdated dependencies, run update to update them.

Why? Don't panic! If you edit composer. json, you will see this information. For example, if you add or update detailed information, such as the Library description, author, more parameters, or even add only one space, the md5sum of the file will be changed. Then Composer will warn you about the differences between the hash value and the records in composer. lock.

So what should we do? The update command can update the lock file, but if only some descriptions are added, it is not intended to update any library. In this case, you only need to update nothing:

$ composer update nothingLoading composer repositories with package informationUpdating dependenciesNothing to install or updateWriting lock fileGenerating autoload files

In this way, Composer will not update the library, but will update composer. lock. Note that nothing is not the keyword of the update command. But the result is not caused by the nothing package. If you enter foobar, the result is the same.

If the Composer version you are using is new enough, you can directly use the -- lock option:

composer update --lock

2. install the library without modifying composer. json

You may think it is too troublesome to modify composer. json every time you install a library, you can directly use the require command.

composer require "foo/bar:1.0.0"

This method can also be used to quickly create a new project. The init command has the -- require option to automatically write composer. json: (note that we use-n, so you don't have to answer questions)

$ composer init --require=foo/bar:1.0.0 -n$ cat composer.json{  "require": {    "foo/bar": "1.0.0"  }}

3. easy to derive

During initialization, have you tried the create-project command?

composer create-project doctrine/orm path 2.2.0

This will automatically clone the repository and check out the specified version. You can use this command to clone a database without searching for the original URI.

4. cache considerations. The dist package takes priority.

In the past year, Composer will automatically archive your downloaded dist package. By default, the dist package is used to add the tag version, for example, "symfony/symfony": "v2.1.4", or a wildcard or version range, "2. 1. * "or"> = 2.2, <2.3-dev "(if you use stable as your minimum-stability.

The dist package can also be used for branches such as dev-master. Github allows you to download a compressed package referenced by git. To forcibly use the compressed package instead of cloning the source code, you can use the -- prefer-dist option of install and update.

The following is an example (I used the -- profile option to display the execution time ):

$ composer init --require="twig/twig:1.*" -n --profileMemory usage: 3.94MB (peak: 4.08MB), time: 0s$ composer install --profileLoading composer repositories with package informationInstalling dependencies - Installing twig/twig (v1.12.2)  Downloading: 100%Writing lock fileGenerating autoload filesMemory usage: 10.13MB (peak: 12.65MB), time: 4.71s$ rm -rf vendor$ composer install --profileLoading composer repositories with package informationInstalling dependencies from lock file - Installing twig/twig (v1.12.2)  Loading from cacheGenerating autoload filesMemory usage: 4.96MB (peak: 5.57MB), time: 0.45s

Here, the twig/twig: 1.12.2 compressed package is saved in ~ /. Composer/cache/files/twig/1.12.2.0-v1.12.2.zip. Use it directly when you reinstall the installation package.

5. modify the source code first.

When you need to modify the library, it is easier to clone the source code than to download the package. You can use -- prefer-source to forcibly choose to clone the source code.

composer update symfony/yaml --prefer-source

Next you can modify the file:

composer status -vYou have changes in the following dependencies:/path/to/app/vendor/symfony/yaml/Symfony/Component/Yaml:  M Dumper.php

When you try to update a modified Library, Composer will remind you to ask if you want to discard the modification:

$ composer updateLoading composer repositories with package informationUpdating dependencies - Updating symfony/symfony v2.2.0 (v2.2.0- => v2.2.0)  The package has modified files:  M Dumper.php  Discard changes [y,n,v,s,?]?

Prepare the production environment

Finally, we will remind you not to forget to optimize automatic loading when deploying code to the production environment:

composer dump-autoload --optimize

You can also use -- optimize-autoloader during the installation package. Without this option, you may find 20% to 25% performance loss.

If you need help or want to know the details of a command, you can read the official documentation or view the notebook memo prepared by JoliCode.


I have a League of Legends, or how can I play well? are there any things I need to know about and tips? I am just a dish, so I am too lazy?

1: Add Troops 2: send various signals 3: Know the target of a group battle you should attack. don't rush into a meat, you will lose all kinds of skills 4: remember various BUFF and Dragon refresh times 5: Know how to plug and buy eyes 6: use the skills that come with various equipment items 7: Know how to support other teammates 8: I know when I should do what I should do. I have seen more than N players, for example: level 10 or so. We killed one enemy. at this moment, the enemy chose to retreat. our teammates didn't know how to plug in the camera and hit a little dragon, but did not dare to jump to the tower when they rushed to the tower. you Q me. i'm asking you to wait until the other party is dead and the person is resurrected and rushed up to destroy us. this is really too much. you need to know when to do things. In short, you need to practice this sentence a lot. clear


Http://www.bkjia.com/PHPjc/865615.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/865615.htmlTechArticlePHPer needs to understand 5 tips for Composer, phpercomposer Composer is a new generation of PHP dependency management tools. Its introduction and basic usage can be found in this article PHP dependency management...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.