Table of Contents
selfupdate" >selfupdate
dumpautoload" >dumpautoload
install" >install
update" >update
require" >require
create-project" >create-project
search" >search
show" >show
config" >config
run-script" >run-script
Home Development Tools composer You must use these Composer commands!

You must use these Composer commands!

Jun 22, 2020 pm 01:43 PM
composer

The following is the tutorial column of composer to introduce you to the common commands of Composer. I hope it will be helpful to friends in need!

You must use these Composer commands!

selfupdate

Update composer itself, please perform composer selfupdate frequently to keep Composer always the latest version .

<span style="font-size: 16px;">composer selfupdate<br/></span>
Copy after login

is equivalent to

<span style="font-size: 16px;">composer self-update<br/></span>
Copy after login

dumpautoload

when we change the composer.json file When autoloading, you need to execute composer dumpautoload to make the autoload take effect immediately. Without having to execute install or update commands.

<span style="font-size: 16px;">composer dumpautoload<br/></span>
Copy after login

is equivalent to

<span style="font-size: 16px;">composer dump-autoload<br/></span>
Copy after login

The dumpautoload command has two commonly used options:

  • --optimize (-o): Convert PSR-0/4 autoloading to classmap for faster loading. This is particularly suitable for production environments, but may take some time to run, so it is not currently the default.
  • --no-dev: Disable autoload-dev rules.

install

<span style="font-size: 16px;">composer install<br/></span>
Copy after login

is defined based on the composer.lock (lock file) or composer.json file in the current directory. Dependencies, install dependent packages.

The install command will first check whether the composer.lock lock file exists. If it exists, it will download the version specified in the composer.lock file, ignoring the definition in the composer.json file.

<span style="font-size: 16px;"># 查看 composer install 的帮助信息<br/>composer install -h<br/><br/># 只安装 require 中定义的依赖,而不安装 require-dev 中定义的依赖<br/>composer install --no-dev<br/></span>
Copy after login

update

If you want to update your dependency version, or you modify the dependency relationship in composer.json, you want composer To perform update operations as defined in the composer.json file, use the update command.

<span style="font-size: 16px;">composer update<br/></span>
Copy after login

require

require command is generally used to install new dependency packages and write dependencies into the composer.json file in the current directory. .

If dependencies are added or changed in the composer.json file, the modified dependencies will be installed or updated.

<span style="font-size: 16px;">composer require<br/></span>
Copy after login

You can also directly specify the dependency packages that need to be installed in the command.

<span style="font-size: 16px;">composer require barryvdh/laravel-ide-helper<br/></span>
Copy after login

Install the specified version of the dependency package.

<span style="font-size: 16px;">composer require barryvdh/laravel-ide-helper:1.0<br/></span>
Copy after login

--dev option corresponds to require-dev. If your dependency package is only used in the development environment, it is recommended to add the --dev option.

<span style="font-size: 16px;">composer require --dev barryvdh/laravel-ide-helper<br/></span>
Copy after login

create-project

You can use create-project to create a new project from an existing package.

It is equivalent to installing the dependencies of this package into its own vendor directory after executing the git clone command.

This command has several common uses:

  • You can quickly deploy your application.
  • You can check out any resource pack and develop patches for it.
  • For multi-person development projects, you can use it to speed up application initialization.
<span style="font-size: 16px;"># 安装 Laravel 项目<br/>composer create-project --prefer-dist laravel/laravel blog 5.5.*<br/></span>
Copy after login

If no version number is specified, the latest version will be installed by default.

--prefer-dist: Install from dist when a package is available.

search

The search command can search for remote dependent packages. Usually it only searches for packages on packagist.org. You can simply enter your search terms.

<span style="font-size: 16px;">composer search monolog<br/></span>
Copy after login

--only-name (-N) option, only searches for the specified name (exact match).

show

#To list installed dependency packages, you can use the show command.

<span style="font-size: 16px;">composer show<br/></span>
Copy after login

List all dependent packages on the mirror site.

<span style="font-size: 16px;">composer show --all<br/></span>
Copy after login

If you want to view detailed information about a specified dependent package, you can enter the package name.

<span style="font-size: 16px;">composer show monolog/monolog<br/></span>
Copy after login

config

config 命令允许你编辑 Composer 的一些基本设置,无论是本地的 composer.json 还是全局的 config.json 文件。

查看 Composer 的全局配置信息:

<span style="font-size: 16px;">composer config -g --list<br/></span>
Copy after login

语法:config [options] [setting-key] [setting-value1] ... [setting-valueN]

setting-key 是配置选项的名称,setting-value1 是配置的值。可以使用数组作为配置的值(像 github-protocols),多个 setting-value 是允许的。

例如,全局配置 Composer 的国内镜像:

<span style="font-size: 16px;"># 推荐使用阿里云 Composer 全量镜像<br/>composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/<br/></span>
Copy after login

选项:

  • --global (-g): 操作 $COMPOSER_HOME/config.json 全局配置文件。如果不指定该参数,此命令将影响当前项目的 composer.json 文件,或 --file 参数所指向的文件。
  • --editor (-e): 使用文本编辑器打开 composer.json 文件。默认情况下始终是打开当前项目的文件。当存在 --global 参数时,将会打开全局的 config.json 文件。
  • --unset; 移除由 setting-key 指定的配置选项。
  • --list (-l): 查看当前配置选项的列表。当存在 --global 参数时,将会显示全局配置选项的列表。
  • --file="..." (-f):在一个指定的文件上操作,而不是 composer.json。它不能与 --global 参数一起使用。

run-script

run-script 命令,可用来手动执行脚本,只需要指定脚本的名称即可。

假如,composer.json 中存在如下脚本。

<span style="font-size: 16px;">{<br/>    "scripts": {<br/>        "post-update-cmd": "MyVendor\\MyClass::postUpdate",<br/>        "post-package-install": [<br/>            "MyVendor\\MyClass::postPackageInstall"<br/>        ],<br/>        "post-install-cmd": [<br/>            "MyVendor\\MyClass::warmCache",<br/>            "phpunit -c app/"<br/>        ]<br/>    }<br/>}<br/></span>
Copy after login

运行所有 post-install-cmd 事件下定义的脚本:

<span style="font-size: 16px;">composer run-script post-install-cmd</span><br/>
Copy after login

更多composer技术文章,请访问composer栏目!                  

The above is the detailed content of You must use these Composer commands!. 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Apr 18, 2025 am 09:24 AM

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

Use Composer to solve dependency injection: application of PSR-11 container interface Use Composer to solve dependency injection: application of PSR-11 container interface Apr 18, 2025 am 07:39 AM

I encountered a common but tricky problem when developing a large PHP project: how to effectively manage and inject dependencies. Initially, I tried using global variables and manual injection, but this not only increased the complexity of the code, it also easily led to errors. Finally, I successfully solved this problem by using the PSR-11 container interface and with the power of Composer.

How to solve the problem of JavaScript error handling using Composer How to solve the problem of JavaScript error handling using Composer Apr 18, 2025 am 08:30 AM

I'm having a tough problem when developing a complex web application: how to effectively handle JavaScript errors and log them. I tried several methods, but none of them could meet my needs until I discovered the library dvasilenko/alterego_tools. I easily solved this problem through the installation of this library through Composer and greatly improved the maintainability and stability of the project. Composer can be learned through the following address: Learning address

How to quickly build LaravelCMS with Composer: mki-labs/espresso practical experience How to quickly build LaravelCMS with Composer: mki-labs/espresso practical experience Apr 18, 2025 am 07:36 AM

I encountered a tricky problem when developing a new Laravel project: how to quickly build a fully functional and easy-to-manage content management system (CMS). I tried multiple solutions, but all gave up because of complex configuration and inconvenient maintenance. Until I discovered the LaravelCMS package mki-labs/espresso, which not only simple to install, but also provides powerful functions and intuitive management interface, which completely solved my problem.

How to use Composer to solve the problem of batch processing of data under Yii framework How to use Composer to solve the problem of batch processing of data under Yii framework Apr 18, 2025 am 07:54 AM

When developing Yii framework projects, you often encounter situations where you need to obtain a large amount of data from the database. If appropriate measures are not taken, directly obtaining all data may cause memory overflow and affect program performance. Recently, when I was dealing with a project on a large e-commerce platform, I encountered this problem. After some research and trial, I finally solved the problem through the extension library of pavle/yii-batch-result.

How to resolve HTTP request issues using Composer: A practical guide to the yiche/http library How to resolve HTTP request issues using Composer: A practical guide to the yiche/http library Apr 18, 2025 am 08:06 AM

During development, HTTP requests are often required, which may be to get data, send data, or interact with external APIs. However, when faced with complex network environments and changing request requirements, how to efficiently handle HTTP requests becomes a challenge. I have encountered a problem in a project: I need to send requests to different APIs frequently, and log the requests to facilitate subsequent debugging and analysis. After trying several methods, I discovered the yiche/http library. It not only simplifies the processing of HTTP requests, but also provides dynamic logging functions, greatly improving development efficiency.

How to use Composer to improve the security of Laravel applications: Applications of wiebenieuwenhuis/laravel-2fa library How to use Composer to improve the security of Laravel applications: Applications of wiebenieuwenhuis/laravel-2fa library Apr 18, 2025 am 11:36 AM

When developing a Laravel application, I encountered a common but difficult problem: how to improve the security of user accounts. With the increasing complexity of cyber attacks, a single password protection is no longer enough to ensure the security of users' data. I tried several methods, but the results were not satisfactory. Finally, I installed the wiebenieuwenhuis/laravel-2fa library through Composer and successfully added two-factor authentication (2FA) to my application, greatly improving security.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

See all articles