search

Composer 是 php 上最优秀的依赖管理工具,所以作为一个文艺的 phper 必须要 get 起来。

上手 == 入坑 ?

不少朋友不愿意上手这货,其实之前我也不太愿意上手这货,最最主要的原因是

说多了都是泪,而且当时各种镜像简直就是坑爹货,无一能用,就算能用,也基本是拼人品的,基本上都不稳定。

甚至当时的我都是手动安装各种包,遇到了N多坑,比如依赖也手动下载,手动导入。。唉。比以前找各种插件各种类的时候还蛋疼万分。

安装 Composer

其实说简单也简单说麻烦也麻烦,因为要分 *nix 和 win 平台,所以直接丢连接自己看好了。

安装 - *nix

安装 - Windows

安装完后测试下是否成功。

$ composer -V

输出类似这样的东西

Composer version 1.0-dev (1a5445132f1cbd366294924dc2fd0114dd4c843a) 2016-03-14 17:39:12

OK,恭喜,可以下一步了。

镜像是什么鬼?

不知道镜像的天朝程序员,不是一个优秀的码农。

由于强力的 GFW 导致无法上一些优秀的国外网站,所以各种镜像服务器诞生了,帮我们把这些网站上的资源都导入到了国内,我们通过修改一个配置就可以下载这些资源了。而且这些资源都是定期同步的,不需要担心资源陈旧问题。

也就是因为这个,N多人望而却步,到现在都不敢用 Composer,感觉好可惜,这么好的工具。

目前唯一还算稳定的 Composer 镜像只有 Packagist / Composer 中国全量镜像这一家了,虽然偶尔抽风。

使用方法也超简单,推荐全局配置镜像。

打开命令行窗口(windows用户)或控制台(Linux、Mac 用户)并执行如下命令:

$ composer config -g repo.packagist composer https://packagist.phpcomposer.com

OK,镜像问题搞定了,接下来才能开始 Composer 的入门学习。

使用 Composer

官方教程虽然全面,但是啰嗦。比如他第一个例子是手动创建 composer.json 然后写入依赖,然后通过 composer install安装json 里的模块包。虽然不能说不对,可现在很少有人这么用,因为太麻烦了。

还是来些常规的用法,比如我想安装 yii2。

根据 Yii官方的安装方法是先安装 Composer Asset 插件:

$ php composer.phar global require "fxp/composer-asset-plugin:~1.1.1"

然后安装基本的应用程序模板:

$ php composer.phar create-project yiisoft/yii2-app-basic basic 2.0.7

或者安装高级的应用程序模板:

$ php composer.phar create-project yiisoft/yii2-app-advanced advanced 2.0.7

但是,我这就出现了无数个这种错误。

Failed to decode response: zlib_decode(): data errorRetrying with degraded mode, check https://getcomposer.org/doc/articles/troubleshooting.md#degraded-mode for more info

百度以后发现加上 --prefer-dist参数即可。

$ php composer.phar create-project yiisoft/yii2-app-advanced advanced 2.0.7 --prefer-dist

PS:如果你安装时卡住了,八成是镜像抽风了,多试几次就好了。

OK,正常安装,然后翻看文档,发现有 --prefer-source和 --prefer-dist参数,对应下载包的两种方式:source(源码) 和 dist(发布),我们当然希望安装 dist(发布)版本啦。

OK,这是安装,那我要添加新的插件,新的依赖这么办?

且听下回分解。。。(′・ω・`)

Statement
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
11 Best PHP URL Shortener Scripts (Free and Premium)11 Best PHP URL Shortener Scripts (Free and Premium)Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram APIIntroduction to the Instagram APIMar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation SurveyAnnouncement of 2025 PHP Situation SurveyMar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software