Home Development Tools composer Composer dependency management (PHP tool)

Composer dependency management (PHP tool)

Jan 26, 2020 pm 04:01 PM
composer

Composer dependency management (PHP tool)

Stop searching everywhere for PHP extension packages. For modern languages, package managers are basically standard. Java has Maven, Python has pip, Ruby has gem, and Nodejs has npm. PHP uses PEAR, but PEAR has many pitfalls:

● Dependency processing is prone to problems

● The configuration is very complicated

● Difficult to use command line interface

Fortunately, we have Composer, a powerful tool for PHP dependency management. It's open source and simple to use, and it's easy to submit your own packages.

For example, if we don’t use a framework at the beginning and want a verification code, we must first go to Gihutb or other places to find a verification code class, then include it in the project, and then edit it As soon as it starts running, problems may arise in later project maintenance. If it is open source on Github, you can click watch to see if there are bug fixes or new versions released, and you can follow up on the upgrades in a timely manner.

If you download too many expansion packs, you will need various includes. There may also be namespace conflicts. You will need to change the namespace according to the project. If the expansion pack is upgraded, you will need to download it again. Edit, this is very inconvenient. So the Composer dependency management library was born.

The above are the more important demand scenarios of Compser.

1. Can easily install and upgrade expansion packs

2. Just include, no need to write include everywhere

3. Avoid namespace conflicts

I usually go to Github, Code Cloud and other platforms to find expansion packages, but now there is a website that integrates all packages. In other words, the current development method: first search on packagist, and then use Composer to install and upgrade.

Install Composer

For fool-proof installation, just click https://getcomposer.org/Composer-Setup.exe, download and install, and the installation program will be You download Composer and set your PATH environment variable so that you can simply call Composer from any directory.

During the installation process, you need to pay attention to finding the root directory of php.exe and selecting the correct PHP path. I won’t provide screenshots here because I haven’t downloaded it yet and I installed it manually.

The selected directory should look like this:

D:\phpStudy\php\php-7.0.12-nts\php.exe
Copy after login

The following will focus on manual installation. I think this method is very easy to use:

First Download a composer.phar file and place the phar file in the developer folder. You can do this as you wish, and there is no limit to which folder it should be placed in.

Then open the DOS window, or use the shortcut key windows R to enter cmd, use the following command, first enter the directory where you placed the phar file

D:\developer\composer>echo @php "%~dp0composer.phar" %*>composer.bat
Copy after login

The sign of successful installation is to enter on the command line

composer -v
Copy after login

Display the following content

Composer dependency management (PHP tool)

##When I see this, I assume that Composer has been installed successfully. In the Chinese LAN, use Composer It is relatively slow, but fortunately there is a domestic mirror. Execute the following command to switch to the domestic mirror. What the domestic mirror does is to cache all installation packages and metadata to the domestic computer room and accelerate it through the domestic CDN, so that there is no need to Make a request to a foreign website.

composer config -g repo.packagist composer https://packagist.phpcomposer.com
Copy after login

Doing this is equivalent to changing the configuration globally. I chose to modify the composer.json configuration file of the current project:

{
    "repositories": {
        "packagist": {
            "type": "composer",
            "url": "https://packagist.phpcomposer.com"
        }
    }
}
Copy after login

Composer Common Commands<strong></strong>

selfupdate<strong></strong>

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

composer selfupdate
Copy after login

Equivalent to

composer self-update
Copy after login

dumpautoload<strong></strong>##When we change the autoload in the composer.json file, we need Execute composer dumpautoload to make autoload take effect immediately. Without having to execute install or update commands.

composer dumpautoload
Copy after login

Equivalent to

composer dump-autoload
Copy after login

dumpautoload command has two commonly used options:

--optimize (-o): Convert PSR-0/4 autoloading to classmap, for faster loading speeds. 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

composer install
Copy after login

According to the composer.lock (lock file) or composer.json file in the current directory, Define dependencies and install dependency 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.

# 查看 composer install 的帮助信息
composer install -h
# 只安装 require 中定义的依赖,而不安装 require-dev 中定义的依赖
composer install --no-dev
Copy after login

update<strong></strong>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.

composer update
Copy after login

require<strong></strong>

require 命令一般用来安装新的依赖包,并将依赖写入当前目录的 composer.json 文件中。

如果 composer.json 文件中,添加或改变了依赖,修改后的依赖关系将被安装或者更新。

composer require

你也可以直接在命令中指明需要安装的依赖包。

composer require barryvdh/laravel-ide-helper
Copy after login

--dev 选项和 require-dev 相对应。如果你的依赖包仅仅用于开发环境,建议加上 --dev 选项。

composer require --dev barryvdh/laravel-ide-helper
Copy after login

<strong>create-project</strong>

你可以使用 create-project 从现有的包中创建一个新的项目。

它相当于执行了 git clone 命令后,将这个包的依赖安装到它自己的 vendor 目录。

此命令有几个常见的用途:

你可以快速的部署你的应用。

你可以检出任何资源包,并开发它的补丁。

多人开发项目,可以用它来加快应用的初始化。

# 安装 Laravel 项目
composer create-project --prefer-dist laravel/laravel blog 5.5.*
Copy after login

如果没有指定版本号,就默认安装最新的版本。

--prefer-dist: 当有可用的包时,从 dist 安装。

phpStudy集成环境下 安装composer失败

报错提示:

The "https://getcomposer.org/versions" file could not be downloaded: failed to open stream: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
The "https://getcomposer.org/download/1.2.0/composer.phar.sig" file could not be downloaded: SSL: crypto enabling timeout
Failed to enable crypto
failed to open stream: operation failed
Copy after login

1、安装composer需要开启openssl拓展 而phpstudy默认是关闭的

2、将php目录下的ssleay32.dll,libeay32.dll以及php/ext文件夹下的:php_openssl.dll 3个文件拷贝到WINDOWS\system32 文件夹下。

3、openssl需要CA证书 phpstudy也是没有的

CA证书下载地址:

http://curl.haxx.se/docs/caextract.html

选中之后单击右键选择另存为

下载成功之后放到tmp文件夹下面

4、然后修改php.ini文件

openssl.cafile = "D:\phpStudy\tmp\cacert.pem"
Copy after login

5、重启phpStudy就可以了报错提示:

failed to open stream: HTTP request failed!
Copy after login

1、检查一下php的curl拓展是否开启

2、检查这两个配置是否开启。

allow_url_fopen = On
user_agent="PHP"
Copy after login

 

也可以这样配置 user_agent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)")

模拟浏览器访问也是一个不错的选择

3、开启之后重启重启phpStudy就可以了

 

PS: openssl.cafile 配置选项, 是 PHP 5.6.0. 以上的版本才支持的

更多composer相关技术文章,请访问composer栏目:https://www.php.cn/tool/composer/

The above is the detailed content of Composer dependency management (PHP tool). 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)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1229
24
Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Apr 18, 2025 am 11:48 AM

When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

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.

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.

How to view the version number of laravel? How to view the version number of laravel How to view the version number of laravel? How to view the version number of laravel Apr 18, 2025 pm 01:00 PM

The Laravel framework has built-in methods to easily view its version number to meet the different needs of developers. This article will explore these methods, including using the Composer command line tool, accessing .env files, or obtaining version information through PHP code. These methods are essential for maintaining and managing versioning of Laravel applications.

Using Dicr/Yii2-Google to integrate Google API in YII2 Using Dicr/Yii2-Google to integrate Google API in YII2 Apr 18, 2025 am 11:54 AM

VprocesserazrabotkiveB-enclosed, Мнепришлостольностьсясзадачейтерациигооглапидляпапакробоглесхетсigootrive. LEAVALLYSUMBALLANCEFRIABLANCEFAUMDOPTOMATIFICATION, ČtookazaLovnetakProsto, Kakaožidal.Posenesko

How to simplify email marketing with Composer: DUWA.io's application practices How to simplify email marketing with Composer: DUWA.io's application practices Apr 18, 2025 am 11:27 AM

I'm having a tricky problem when doing a mail marketing campaign: how to efficiently create and send mail in HTML format. The traditional approach is to write code manually and send emails using an SMTP server, but this is not only time consuming, but also error-prone. After trying multiple solutions, I discovered DUWA.io, a simple and easy-to-use RESTAPI that helps me create and send HTML mail quickly. To further simplify the development process, I decided to use Composer to install and manage DUWA.io's PHP library - captaindoe/duwa.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

See all articles