What technology is PHP composer?

If you are a programmer, you are likely to find that the wheel of technology update is too fast for you to keep up with, such as user authentication, database management, routing and other technologies. PHP has many mature frameworks to deal with these problems, so you think these frameworks should be easy to use, don’t you?

If you manually install Zend, laravel or symfony now, you will find that it is not an easy thing. Each technology has its own various dependency packages, so things are in a mess in the end.

At this time, the function of composer appears. Composer is a dependency management tool for PHP. It manages all the dependencies your PHP project needs. In other words, composer will pull all the code bases and dependencies needed by your PHP project from the Internet and put them together for management.

This kind of dependency management for projects is not a new concept. In fact, composer is favored by NPM node.js And bundler, inspired by ruby.

I believe you should know something about pear. Pear is a PHP library management tool that existed many years ago. However, many programmers don’t like to use pear for many reasons. First, it is out of date. Second, pear requires you to install the whole system instead of each project. That is to say, if you have a project that is dependent on a slightly older library, things are in trouble. To learn more about the history of PHP package management tools, read the article packages: the way forward for PHP.

How to install composer

The installation of composer is very simple and can be completed with a few lines of command. I use OS X, but it’s the same for any * Nix system.

Please open the command line window and run the following command:

$ curl -s https://getcomposer.org/installer | php  
$ sudo mv composer.phar /usr/local/bin/composer  

The first command is to composer.phar Download the file to your computer. The second command is to composer.phar Move the file to your bin directory so that you can access it globally on your computer.

Now run the following command:

$ composer  

If you successfully install composer, this command will return you a list of valid command parameters and usage description.

How to install composer on Windows

If you want to install composer in Windows system, you can find the installation method on the official website, http://getcomposer.org/ .

How to use composer?

Now that composer has been installed on our computer, we can start to use it to import the code base needed in the project. Composer needs a configuration file, which is a JSON file. We put it in the root directory of the project.

For example, if we want to use slim framework, we can create the following composer.json File:

"require": {  
"slim/slim": "2.*"  

To install slim through composer, you just need to use the following command:

 $ composer install  

Now slim will be downloaded automatically and installed in the vendor / slim / slim directory.

Isn’t it simple!


Now, suppose your project depends on many different code libraries, and you want to be able to load them automatically. Fortunately, there is a file in composer that can perform this task. It can automatically download all the libraries that your project depends on to the project.

To use composer’s autoloader, you only need to include a line in the relative index file or startup file

 require ‘vendor/autoload.php’;  

This way, you don’t have to worry about adding new dependency packages to your project.

For example:

// Autoload  
require ‘vendor/autoload.php’;

// 实例或 Slim 应用  
$app = new SlimSlim();

// 定义 HTTP GET 路径 
$app->get(‘/hello/:name’, function ($name) {  
echo "Hello, $name";  

// 运行 Slim 应用  


Using package management tools in PHP projects is the right direction. Programming languages like ruby have shown us the convenience of using dependency management tools in projects, saving a lot of developers’ development and configuration time.

A large number of popular frameworks also start to use composer, and more developers use composer in their own projects.

As a PHP developer, composer will be your best partner. With the increase of usage, composer will become the basic library of PHP.

With the increase of good tools like composer, PHP will become better and better. Composer has solved many big problems for the PHP community, and there’s no reason why we still refuse to use it.

Related Article
WordPress 3D rotating tag cloud

This 3D tag cloud is not the flash version of WP cumulus n years ago. This 3D rotating tag cloud Read more

Using wampserver tool to build local PHP + MySQL environment

No matter whether we use domestic space or foreign hosts, we prefer to go online after local debugging before setting Read more

How to install Linux system on Android phone

In our daily use of Android mobile phones, the installation of a complete Linux system will make mobile phones become Read more

How to backup your website data with Android phone

No matter what you do, backup is very necessary and important. Backup is just in case. Most of the time, Read more

Leave a Reply

Your email address will not be published. Required fields are marked *

To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image